Search code examples
javajsonjson-simple

Parsing JsonArray inside JsonArray(nested)


I make a call to REST services and it returns a json string, looks like:

{
   "head":[
          "\"head1\"", 
          "head2",
          "head3"
    ],
   "tail":[
            [
               "tail1",
               "tail2",'
               "tail3"
            ],
            [
               "tail1a",
               "tail2a", 
               "tail3a"
            ],
            ....and so on till n.
          ]
}

I would like to parse the json in such a way that, I get a (key,value) pair. I.e head1=tail1, head2=tail2, head1=tail1a, head2=tail2a and so on.

Is there a way to achieve this? Also suggest which json should I use, I have found 3 types of .jar files over internet and totally confused.


Solution

  • I am using org.json package, to read more about org.json click here

    Example code

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class Example {
    
    public static void main(String[] args) {
        String jsonString = "{\"head\":[\"head1\",\"head2\",\"head3\"],\"tail\": [[\"tail1\",\"tail2\",\"tail3\"],[\"tail1a\",\"tail2a\",\"tail3a\"]]}";
        try {
            JSONObject mainJsonObject = new JSONObject(jsonString);
            JSONArray headJsonArray = mainJsonObject.getJSONArray("head");
            JSONArray tailJsonArray = mainJsonObject.getJSONArray("tail");
            Map<String, List<String>> keyValue = new HashMap<String, List<String>>();
            int index = 0;
            for (int i = 0; i < headJsonArray.length(); i++) {
                String head = headJsonArray.getString(i);
                List<String> values = new ArrayList<String>();
                for (int j = 0; j < tailJsonArray.length(); j++) {
                    JSONArray anotherTailArray = tailJsonArray.getJSONArray(j);
                    values.add(anotherTailArray.getString(index));
                }
                keyValue.put(head, values);
                index++;
            }
    
            for (String key: keyValue.keySet()) {
                List<String> values = keyValue.get(key);
                for (int i = 0; i < values.size(); i++) {
                    System.out.println(key + " = " + values.get(i));
                }
            }
    
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
    }
    }
    

    Output

    head1 = tail1
    head1 = tail1a
    
    head2 = tail2
    head2 = tail2a
    
    head3 = tail3
    head3 = tail3a
    

    This is just an example, you can refactor according to your need.