0

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.

srikanth
  • 958
  • 16
  • 37
  • 1
    As for the JSON libs you should read their documentation and test them. There are a lot of libs that basically have the same set of features (like Json-Simple, Gson, Jackson, etc.) As for the mapping: you'd have to do it yourself. On a base level you can think of JsonObject as a map and of JsonArray as, well, an array. If you mix them you have maps in maps in arrays of maps etc. - So just try something on that ground and show us what you got. – Thomas Jun 17 '16 at 10:49

1 Answers1

0

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.

Ravikumar
  • 891
  • 12
  • 22