Nishant Tanwar Nishant Tanwar - 3 years ago 218
Android Question

Parse json without knowning the keys

i am trying to parse json in java without knowing the keys and the structure of json format and save that data into the hashmap

how would i cycle through the whole json format and store key and value into the hashmap

{"id" : 12345, "value" : "123", "person" : "1"}


like in this example all the keys would be jsonobject none of them will be json array

There is some other library like jackson i don't want to use any third party library

Answer Source

This is Just an Example. For a JSON Like

{
 "status": "OK",
 "search_result": [

            {
                "product": "abc",
                "id": "1132",
                "question_mark": {
                    "141": {
                        "count": "141",
                        "more_description": "this is abc",
                        "seq": "2"
                    },
                    "8911": {
                        "count": "8911",
                        "more_desc": "this is cup",
                        "seq": "1"
                    }
                },
                "name": "some name",
                "description": "This is some product"
            },
            {
                "product": "XYZ",
                "id": "1129",
                "question_mark": {
                    "379": {
                        "count": "379",
                        "more_desc": "this is xyz",
                        "seq": "5"
                    },
                    "845": {
                        "count": "845",
                        "more_desc": "this is table",
                        "seq": "6"
                    },
                    "12383": {
                        "count": "12383",
                        "more_desc": "Jumbo",
                        "seq": "4"
                    },
                    "257258": {
                        "count": "257258",
                        "more_desc": "large",
                        "seq": "1"
                    }
                },
                "name": "some other name",
                "description": "this is some other product"
            }
       ]
}

Use JSONObject keys() to get the key and then iterate each key to get to the dynamic value.

Roughly the code will look like:

// searchResult refers to the current element in the array "search_result"
JSONObject questionMark = searchResult.getJSONObject("question_mark");
Iterator keys = questionMark.keys();

while(keys.hasNext()) {
    // loop to get the dynamic key
    String currentDynamicKey = (String)keys.next();

    // get the value of the dynamic key
    JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);

    // do something here with the value...
}

Try this Code Too

public void parse(String json)  {
   JsonFactory factory = new JsonFactory();

   ObjectMapper mapper = new ObjectMapper(factory);
   JsonNode rootNode = mapper.readTree(json);  

   Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields();
   while (fieldsIterator.hasNext()) {

       Map.Entry<String,JsonNode> field = fieldsIterator.next();
       System.out.println("Key:"field.getKey() + "\tValue:" + field.getValue());
   }

}

also give a look at this link

https://github.com/alibaba/fastjson

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download