7

I have the following JSON:

{
 "registration": {
 "name": "Vik Kumar",
 "first_name": "Vik",
 "last_name": "Kumar",
 "bloodGroup": "B-",
 "gender": "male",
 "birthday": "10\/31\/1983",
 "email": "vik.ceo\u0040gmail.com",
 "cellPhone": "1234123456",
 "homePhone": "1234123457",
 "officePhone": "1234123458",
 "primaryAddress": "jdfjfgj",
 "area": "jfdjdfj",
 "location": {
 "name": "Redwood Shores, California",
 "id": 103107903062719
 },
 "subscribe": true,
 "eyePledge": false,
 "reference": "fgfgfgfg"
 }
}

I am using the following code to parse it:

JsonNode json = new ObjectMapper().readTree(jsonString);
JsonNode registration_fields = json.get("registration");
Iterator<String> fieldNames = registration_fields.getFieldNames();
while(fieldNames.hasNext()){
 String fieldName = fieldNames.next();
 String fieldValue = registration_fields.get(fieldName).asText();
 System.out.println(fieldName+" : "+fieldValue);
}

This works fine and it print all the values except for location which is kind of another level of nesting. I tried the same trick as above code to pass json.get("location") but that does not work. Please suggest how to make it work for location.

JRomero
4,8781 gold badge32 silver badges49 bronze badges
asked Sep 15, 2012 at 0:56
2
  • What do you mean by "does not work", do you get an error message? If so, what does it say? Commented Sep 15, 2012 at 1:04
  • 1
    Just to be clear the above code works fine. but when i try to apply the same logic for location field then the line location_fields.getFieldNames() throws null pointer exception. I am sure i am passing the right name "location" on the first line Commented Sep 15, 2012 at 1:06

2 Answers 2

15

You need to detect when you are dealing with a (nested) Object using JsonNode#isObject:

public static void printAll(JsonNode node) {
 Iterator<String> fieldNames = node.getFieldNames();
 while(fieldNames.hasNext()){
 String fieldName = fieldNames.next();
 JsonNode fieldValue = node.get(fieldName);
 if (fieldValue.isObject()) {
 System.out.println(fieldName + " :");
 printAll(fieldValue);
 } else {
 String value = fieldValue.asText();
 System.out.println(fieldName + " : " + value);
 }
 }
}

Thus, when you reach an object, such as location, you'll call the printAll recursively to print all its inner values.

org.codehaus.jackson.JsonNode json = new ObjectMapper().readTree(jsonString);
org.codehaus.jackson.JsonNode registration_fields = json.get("registration");
printAll(registration_fields);
answered Sep 15, 2012 at 1:05
Sign up to request clarification or add additional context in comments.

1 Comment

node.getFieldNames() is not available. Use node.fieldNames() instead?
1

Since location is nested within registration, you need to use:

registration_fields.get("location");

to get it. But isn't it already processed by the while-loop, why do you need to get it separately?

answered Sep 15, 2012 at 1:05

2 Comments

He wants to print the contents of location. Since it's an object, its text value will be empty.
well true but in that case how do i get those values? the current code line String fieldValue = registration_fields.get(fieldName).asText() prints blank

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.