BRDroid
BRDroid

Reputation: 4388

How to extract JSON from hashMap and add it to another JSONObject ?

I have a HashMap in which I am saving JSONObject dynamically. There can be more than one JSONObjects in the hashMap. I want to extract them and add it to another JSONObject.

At the moment I am iterating through the map and extracting the JSONObjects and adding it to a String.

String personData = "";    
Iterator myVeryOwnIterator = map.keySet().iterator();
while(myVeryOwnIterator.hasNext()) 
{
    Integer key=(Integer)myVeryOwnIterator.next();
    JSONObject value;
    value= map.get(key);                    
    personData = personData + value.toString();                 
}

I tried creating another JSONObject to convert string 'personData' to JSONObject and add it to the main JSONObject incidentJson. But it has only one value, but String personData can have more than one JSON data.

JSONObject personDetailsJSON = new JSONObject(personData);
incidentJson.put("PersonDetails", personDetailsJSON);

Upvotes: 2

Views: 110

Answers (1)

cosmincalistru
cosmincalistru

Reputation: 1325

Try like this :

JSONObject personData = new JSONObject();

while (...) {
    Integer key=(Integer)myVeryOwnIterator.next();
    JSONObject value;
    value= map.get(key);                    
    personData.put(""+key, value);
}

Upvotes: 0

Related Questions