sarma senagavaram sarma senagavaram - 2 months ago 8x
reST (reStructuredText) Question

how to get json keys when im getting the json content in string format?

Im getting my json in string format from the URI .

String srvdiagJson = getJsonFromHttpConnection(diagcon);

my json data looks like this.


I think there is no need of parsing the data.But i also tried parsing it on advice and kept it in both object and array. but i am unable to access any values.


Using Jackson? You could try one of the following approaches to get the keys from your JSON:

Parsing JSON into a JsonNode

ObjectMapper mapper = new ObjectMapper();

JsonNode root = mapper.readTree(json);

Iterator<String> it = root.fieldNames();
while (it.hasNext()){

Parsing JSON into a Map<String, Object>

ObjectMapper mapper = new ObjectMapper();

TypeReference<LinkedHashMap<String, Object>> typeReference 
    = new TypeReference<LinkedHashMap<String, Object>>() {};
Map<String, Object> root = mapper.readValue(json, typeReference);


Both solutions require jackson-databind dependency.