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);
2
  • try dropping the ""+ part, you don't need to convert things to strings Commented Sep 11, 2017 at 12:15
  • you can use obj.put("business_images", Arrays.toString(business_images) ); Commented Sep 11, 2017 at 12:15

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

1 Comment

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
0
 ObjectMapper mapper = new ObjectMapper();
   System.out.println(mapper.writeValueAsString(list));

2 Comments

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
@BalagurunathanMarimuthu thanks for teach me next time defiantly i remember this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.