I have a JSON response which look something like this( its different from user to user)
{
"success": true, "data":
[
{
"mandatory_tag": "My Company",
"id": "topic_1408946825893148"
},
{
"mandatory_tag": "Partners",
"id": "topic_1408946942491149",
},
{
"mandatory_tag": "Industry",
"id": "topic_1408946996510150",
},
{
"mandatory_tag": "Competitors",
"id": "topic_1409210454810358",
},
{
"mandatory_tag": "Competitors",
"id": "topic_1408947133657152"
},
{
"mandatory_tag": "Competitors",
"id": "topic_1408947071457151",
},
{
"mandatory_tag": "Competitors",
"id": "topic_1409210621754362",
},
{
"mandatory_tag": "Competitors",
"id": "topic_1409210704390363",
},
{
"mandatory_tag": "Competitors",
"id": "topic_1409210794791364"
}
]
}
I am parsing it and trying to store in HashMap, but the key value is duplicating. Can anyone suggest me how can i store all id with same mandatory_tag in one array?
I am new to this so please consider..thanks
try { JSONObject jsonMain = new JSONObject(jsonString); JSONArray dataArray = jsonMain.getJSONArray("data"); for(int i = 0; i < dataArray.length(); i++) { JSONObject tagObject = dataArray.getJSONObject(i); String mandatory_tag = tagObject.getString("mandatory_tag"); String id = tagObject.getString("id"); List<String> arrayID = new ArrayList<String>(); if(myMap.containsKey(mandatory_tag)) { arrayID.add(id); myMap.put(mandatory_tag, arrayID); } else { List<String> newArrayID = new ArrayList<String>(); newArrayID.add(id); myMap.put(mandatory_tag, newArrayID); } }
And well i got stuck at this now..any good logic please..