3

I am Trying to parse nested JSON, But i don't know the format of JSON...I want all the Keys and Values of JSON.

Example:

{
    "txnSpecificData1": "11",
    "txnSpecificData2": "21",
    "merchantData": {
        "merchantSpecificData1": "111",
        "merchantSpecificData2": "222",
        "merchantSpecificData3": {
            "data1": "1",
            "data2": "2"
        }
    }
}

Now I want all Keys and their Values...I tried JSONParser but it is not giving all keys. Please some one guide me to do this.

I need value based on key.. Let's say If i give key as "merchantSpecificData2" it should return me value.

O/P should be MAP with all Key and Values:

OutPut Map : : : {txnSpecificData1=11, txnSpecificData1=22, merchantSpecificData1=111, merchantSpecificData2=222,data1=1,data2=2}
user2888996
  • 417
  • 8
  • 20
  • Your question is ill-formed. What result should asking for `"a"` from `{"a": 1, "b": {"a": 2}}` give? – Eric Oct 29 '15 at 06:28
  • @Eric I want all key with values store in a map. txnSpecificData1:11 txnSpecificData2:21 merchantSpecificData1:111 merchantSpecificData2:222 like wise – user2888996 Oct 29 '15 at 06:30
  • You've just ignored my question and restated yours even less clearly. Notice how in the "map" I gave above, the key "a" appears twice. Which value do you want, and why? – Eric Oct 29 '15 at 06:46
  • @Eric..In my case there is no duplicate keys..why are you asking me this? – user2888996 Oct 29 '15 at 06:50
  • You specifically said that you _"don't know the keys"_. So how do you know they're all unique? – Eric Oct 29 '15 at 13:34

4 Answers4

3

A iterative solution to store all keys and respective value into a map. Note, all exception scenarios are not handled.

public static void main(String[] args) throws Exception{
    JSONObject jo = new JSONObject(your_json_String_here);
    Map<String, String> map = new HashMap<String, String>(); 
    new Test().iterateJson(jo, map);
    System.out.println(map);
}
public void iterateJson(JSONObject jo, Map map) {
    for(Object o : jo.keySet()){
        if(jo.get(o.toString()) instanceof JSONObject) 
            iterateJson(jo.getJSONObject(o.toString()), map);
        else
            map.put(o.toString(), jo.get(o.toString()));
    }
}
boardtc
  • 1,304
  • 1
  • 15
  • 20
Naveen Ramawat
  • 1,425
  • 1
  • 15
  • 27
2

Please refer this

Sample Code

public static void main(String[] args) throws JSONException {
        String json = "{\"txnSpecificData1\": \"11\",\"txnSpecificData2\": \"21\",\"merchantData\": {\"merchantSpecificData1\": \"111\",\"merchantSpecificData2\": \"222\",\"merchantSpecificData3\": {\"data1\": \"1\",\"data2\": \"2\"}}}";

        JSONObject jsonObject = new JSONObject(json);
        JSONObject merchantData = (JSONObject) jsonObject.get("merchantData");
        System.out.println(merchantData.get("merchantSpecificData2"));
    }

Output

222

Jar

Maven

Refer hew for more details.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • ...I have done this..but as i said i don't know the format..I mean instead of "merchantData" it can be anything – user2888996 Oct 29 '15 at 06:08
  • @user2888996 maybe using `Regex` looks weird but might solve – Ankur Singhal Oct 29 '15 at 06:09
  • Okay...lF i use this only..then how can i find value for key "txnSpecificData1" – user2888996 Oct 29 '15 at 06:21
  • `jsonObject.get("txnSpecificData1");`, also you can always iterate recursivley the `jsonObject`, check for instance if it is array or object – Ankur Singhal Oct 29 '15 at 06:52
  • @ ankur-singhal...I want all the keys to be stored in Map with their respective values..and i don't know any of the key also level of nesting – user2888996 Oct 29 '15 at 06:53
  • @user2888996 i got your point, answer edited, please click on link `Refer hew for more details.`, it will convert to `Map structure - key-value` pair – Ankur Singhal Oct 29 '15 at 07:19
  • @ankur-songhal..i already tried this..i am getting this as MAP...now how can i find key here ??? {txnSpecificData2=21, merchantData={merchantSpecificData1=111, merchantSpecificData2=222, merchantSpecificData3={data1=1, data2=2}}, txnSpecificData1=11} – user2888996 Oct 29 '15 at 07:27
  • ...@help me ..I am stuck – user2888996 Oct 29 '15 at 08:37
0

You can use http://json-lib.sourceforge.net/apidocs/net/sf/json/package-summary.html for parsing the JSON. Using JSONSerializer you can get a JSONObject parsing your string. Then methods available in JSONObject will give you all keys and values. Value can be simple like String/Long/Boolean or complex like another JSONObject/JSONArray.

Dev Blanked
  • 8,555
  • 3
  • 26
  • 32
0

Here is the solution..

private static HashMap print(final JsonNode node) throws IOException {
        HashMap map = new HashMap();
        Iterator<Map.Entry<String, JsonNode>> fieldsIterator = node.getFields();

        while (fieldsIterator.hasNext()) {
            Map.Entry<String, JsonNode> field = fieldsIterator.next();
            final String key = field.getKey();

            final JsonNode value = field.getValue();
            if (value.isContainerNode()) {
                print(value);
            } else {
                map.put(key, value);
            }
        }
        return map;
    }
user2888996
  • 417
  • 8
  • 20