Skip to content Skip to sidebar Skip to footer

Parse.com Add Json Object To Json Array

My problem is simple, I'd like to add a JSONObject to a JSONArray that I store in a MongoDB database. I can easily add all types of data like Strings, Ints etc but not JSONObjects.

Solution 1:

Try using object.toString() instead of object.

lan.add("Participants", object.toString());

JSON:

{"Participants":["{\"PlayerName\":\"John\",\"ID\":514145}"]}

To parse this JSON try this:

JSONObject jsonObj = newJSONObject(YOUR_JSON_STRING);

// ParticipantsJSONArray participantsJsonArray = jsonObj.getJSONArray("Participants");

// ParticipantJSONObject participanJsonObject = participantsJsonArray.getJSONObject(0);

// PlayerNameString playerName = participanJsonObject.getString("PlayerName");
// IDString id = participanJsonObject.getInt("ID");

Hope this will help~

Solution 2:

Convert that Json Object into String and store it in string format

lan.add("Participants",object.toString());

And when you want to use that you can easily convert it into Json Object again like this

JSONObject jObj=newJSONObject("Your Json String");

Post a Comment for "Parse.com Add Json Object To Json Array"