3

I am newbie in android development and I am trying to get JSONArray from a android List. (I need a org.json.JSONArray so i am unable to do it with Gson).

I have tried to find this on SO and found a wonderful solution here.

So far i am able achieve this partially (for string and Integer values). But I have a String[] values in my List. how can i convert this in JSONObject?

here's how i implemnted my code to convert JSONArray:

JSONArray jsonArray = new JSONArray();

     for (int i = 0; i < modifiedList.size(); i++) {
         jsonArray.put(modifiedList.get(i).getJSONObject());
     }

here's getJSOnObject method implemetation in my Object Class:

public JSONObject getJSONObject() {
        JSONObject obj = new JSONObject();
        try {
            obj.put("chat_id", ""+chat_id);
            obj.put("latest_chat_message", ""+latest_chat_message);
            obj.put("user_id", ""+user_id);
            obj.put("business_id", ""+business_id);
            obj.put("chatting_user_id", ""+chatting_user_id);
            obj.put("latest_chat_fullname",""+ latest_chat_fullname);
            obj.put("latest_chat_user_id", ""+latest_chat_user_id);
            obj.put("chat_title", ""+chat_title);
            obj.put("views", ""+views);
            obj.put("created_on1", ""+created_on1);
            obj.put("created_on",""+ created_on);
            obj.put("chat_image", ""+chat_image);
            obj.put("chat_comment", ""+chat_comment);
            obj.put("chat_notification", ""+chat_notification);
            obj.put("chat_expiry", ""+chat_expiry);
            obj.put("del_in", ""+del_in);
            obj.put("customer_id", ""+customer_id);
            obj.put("customer_fullname", ""+customer_fullname);
            obj.put("customer_photo", ""+customer_photo);
//            obj.put("business_images", business_images);
//            obj.put("user_names", user_names);
            obj.put("isFav", ""+isFav);
//            obj.put("user_images", user_images);
            obj.put("isRead", ""+isRead);

        } catch (JSONException e) {

        }
        return obj;
    }

here "business_images", "user_names" are string[] so this will return [Ljava.lang.String;@596ef8d kind of address value instead of values so the question is how can i get this values instead of address?

EDIT:

none of these solution worked for me as Arrays.tostring(String[]) returns String instead of String[]

so for loop solved my problem here's how I implemented:

JSONArray userNames = new JSONArray();

for (String user_image : user_images) {
                userImages.put(user_image);
            }

obj.put("user_images", userImages);
Hiren Nakrani
  • 234
  • 3
  • 15

2 Answers2

2

Since arrays are objects in java and being treated as references instead of values so to get the string representation of your array you can use Arrays#toString

obj.put("business_images", Arrays.toString(business_images) );

To get JSONArray use

obj.put("business_images", new JSONArray(Arrays.toString(business_images)));

and since JSONObject support int,long,double,boolean so you don't need to promote primitive to String but if your REST API expecting all as string then use String.valueOf for performance efficiency

obj.put("chat_id", String.valueOf(chat_id));
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • sorry for my late response, But as per your suggestion i got values but in single string i mean i got string ("[a,b,c]") instead of array([a, b, c]). please help me to get this kind of array – Hiren Nakrani Sep 12 '17 at 04:43
0
 ObjectMapper mapper = new ObjectMapper();
   System.out.println(mapper.writeValueAsString(list));
Sandip Solanki
  • 704
  • 1
  • 8
  • 15
  • 2
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Balagurunathan Marimuthu Sep 11 '17 at 13:51
  • @BalagurunathanMarimuthu thanks for teach me next time defiantly i remember this – Sandip Solanki Sep 11 '17 at 13:54