Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert List<object> into org.json.JSONArray?

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);
like image 581
Hiren Nakrani Avatar asked Jul 31 '26 22:07

Hiren Nakrani


1 Answers

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));
like image 122
Pavneet_Singh Avatar answered Aug 03 '26 13:08

Pavneet_Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!