How to get all key and values from nested JSON in java
Hi I need to read all key, values from nested JSON, where ever there is inner JSON. I need that values ignoring the key.From below JSON i need Key values for nested JSON, like: responseStatus-passed, "statusCode":"200", "retrieveQuoteResponse":null,"quoteGuid":null, etc.ignoring the start key value like: responsePreamble, quoteProductList which has a nested json inside it.
{
"responsePreamble": {
"responseStatus": "Passed",
"statusCode": "200",
"responseMessage": "Records Found"
},
"retrieveQuoteResponse": null,
"totalQuoteProductCount": 2,
"quoteProductList": {
"quoteGuid": null,
"quantity": 180
}
Code:
ObjectReader reader = new ObjectMapper().readerFor(Map.class);
Map<String, Map<String, String>> employeeMap = reader.readValue(jsonObject);
for (Entry<String, Map<String, String>> empMap : employeeMap.entrySet()) {
Map<String, String> addMap = empMap.getValue();
if(addMap!=null) {
for (Entry<String, String> addressSet : addMap.entrySet()) {
System.out.println(addressSet.getKey() + " :: " + addressSet.getValue());
}
}
}
OutPut:
responseStatus :: Passed
statusCode :: 200
responseMessage :: Records Found
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
at com.im.api.tests.CompareTwoJsons.main(CompareTwoJsons.java:78)
Comments
Post a Comment