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);