1

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..

4 Answers 4

1

A hashmap allows you to store key-value items. I would suggest the HashMap be of type <String,List<String>> so allowing you to store items with the same mandatory_tag in the same list under 1 key.

For example

HashMap<String,List<String>> myMap = new HashMap<String,List<String>>();

if (myMap.containsKey(mandatory_tag)) {
    List<String> values = myMap.get(mandatory_tag);
    if (values!=null) {
       values.add(id)
    } else {
        values = new List<String>();
        values.add(id);
    }
}

Update

As I wrote in my comment below, the except you added has an error

List<String> arrayID = new ArrayList<String>();
                        if(myMap.containsKey(mandatory_tag))
                        {
                            arrayID.add(id);
                            myMap.put(mandatory_tag, arrayID);
                        }

What you are doing here is, if the map already contains the key you are replacing the value associated with the key with a new list which contains only 1 value. What you need to do is update the list of already existing values. Check the code below and make sure you understand the problem. These are fundamentals of programming and you need to understand the logic of what you're doing to advance.

//Somewhere you have declared your HashMap
HashMap<String,List<String>> myMap = new HashMap<String,List<String>>();

//then we continue with your excerpt  
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");

    if(myMap.containsKey(mandatory_tag)) {
         List<String> arrayID = myMap.get(mandatory_tag);
         arrayID.add(id);
    } else {
         List<String> newArrayID = new ArrayList<String>();
         newArrayID.add(id);
         myMap.put(mandatory_tag, newArrayID);
    }

}

Sign up to request clarification or add additional context in comments.

4 Comments

can u paste the code u are currently using to parse the JSON? mandatory_tag is a variable containing the mandatory_tag of the parsed JSON. myMap is the HashMap you create. The functions for HashMap are available here So you would need the containsKey(key) function
Hey Young, this is what i am trying now.. 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"); if(myMap.containsKey(mandatory_tag)) { List<String> values = myMap.get(mandatory_tag); if (values!=null) {values.add(id); } else { values = new ArrayList<String>(); values.add(id);}}}
@Julian is it not working for you? Please edit your original comment with the properly formatted code.
@Julian your error start at List<String> arrayID = new ArrayList<String>(); What you are doing here and in the following if statement, when the condition is true, is that you are not adding your id to the already existing list of values but to a new list, and then replacing the old list with the new one. I've updated my original answer with the code.
0

Create a POJO called TagValues(or whatver name you would like) with variables mandatory_tag,id and then and create an ArrayList<TagValues> in which you can store all the objects.

1 Comment

hey apoorv, thanks, but can you give some related reference..it wil be more helpfull.
0

Thanks for your response, but with help of you guys, i got the solution

`HashMap<String,List<String>> myMap = new HashMap<String,List<String>>();    
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");
        if(myMap.isEmpty())
        {
            arrayID = new ArrayList<String>();
            arrayID.add(id);
            myMap.put(mandatory_tag, arrayID);
        } 
        else if(myMap.containsKey(mandatory_tag))
        {
            arrayID.add(id);
            myMap.put(mandatory_tag, arrayID);
        }
        else
        {
            arrayID = new ArrayList<String>();
            arrayID.add(id);
            myMap.put(mandatory_tag, arrayID);
        }
    }`

1 Comment

you don't need to check if the map is empty. Also in the second if statement, where are you getting the arrayID from?
0

In those cases, I'd like to use a custom HashMap:

public MMap getData() {
    MMap result = new MMap();
    JSONArray array = getJSONArrayFromTotalJSON();
    for (int i = 0; i < array.length(); i++) {
        JSONObject obj = array.getJSONObject(i);
        String mtag = obj.getString(MANDATORY_TAG);
        String id = obj.getString(ID);
        result.put(mtag, id);
    }
    return result;
}

public class MMap extends HashMap<String, List<String>> {
    public void put(String key, String value) {
        if (!this.containsKey(key))
            this.put(key, new ArrayList<String>());
        this.get(key).add(value);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.