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,878
  • 1
  • 27
  • 49
Vik
  • 8,721
  • 27
  • 83
  • 168
  • What do you mean by "does not work", do you get an error message? If so, what does it say? – Hunter McMillen Sep 15 '12 at 01: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 – Vik Sep 15 '12 at 01:06

2 Answers2

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);
João Silva
  • 89,303
  • 29
  • 152
  • 158
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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    He wants to print the contents of `location`. Since it's an object, its *text* value will be empty. – João Silva Sep 15 '12 at 01:07
  • 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 – Vik Sep 15 '12 at 01:08