I have a map of string objects and keys that I wish to put to a json file. I have read that the way to do it is by converting it to an array, and it only works with maps where both the object and key are strings. I can create a JSONObject from the map fine, but cannot put that to an array. Can someone tell me why this does not work?
private static final String JSON_USER_VOTES = "user_votes"; private Map<String, String> mCheckedPostsMap; //This is populated elsewhere JSONObject obj=new JSONObject(mCheckedPostsMap); JSONArray array=new JSONArray(obj.toString()); // <<< Error is on this line json.put(JSON_USER_VOTES, array); Here is the error:
org.json.JSONException: Value {"232":"true","294":"true"} of type org.json.JSONObject cannot be converted to JSONArray
obj.toString()is incorrect. Perhaps it should simply be:array = new JSONArray().put(obj);?