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}
5
  • Your question is ill-formed. What result should asking for "a" from {"a": 1, "b": {"a": 2}} give? Commented Oct 29, 2015 at 6:28
  • @Eric I want all key with values store in a map. txnSpecificData1:11 txnSpecificData2:21 merchantSpecificData1:111 merchantSpecificData2:222 like wise Commented Oct 29, 2015 at 6: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? Commented Oct 29, 2015 at 6:46
  • @Eric..In my case there is no duplicate keys..why are you asking me this? Commented Oct 29, 2015 at 6:50
  • You specifically said that you "don't know the keys". So how do you know they're all unique? Commented Oct 29, 2015 at 13:34

4 Answers 4

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()));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It's not suitable for my case
This example prints all the values of available keys in your JSON string as you requested. It is expected a bit modification if your requirement are little different, Please elaborate more where you stuck
I modified the code a bit, now it is populating a Map of all key value pairs and you can get value of any key using that map
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.

8 Comments

...I have done this..but as i said i don't know the format..I mean instead of "merchantData" it can be anything
@user2888996 maybe using Regex looks weird but might solve
Okay...lF i use this only..then how can i find value for key "txnSpecificData1"
jsonObject.get("txnSpecificData1");, also you can always iterate recursivley the jsonObject, check for instance if it is array or object
@ 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
|
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.

1 Comment

..This library i am not using also i am not getting how can i implement.
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;
    }

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.