1

I'm working with a JSON file that has nested objects like this,

{
  "LocId":99,
  "typeId":99,
  "name":"foo",
  "parentId":99,
  "geoCode":
  {
    "type":"bang",
    "coordinates":
    [{
       "latitude":99.0,
       "longitude":99.0
    }]
  }
}

I created a container to hold the JSON file in a class like this,

public class Location_JSON {

   private LocId id;

   // +getter+setter

   @Override
   public String toString() {
       return id.toString();
   }

   public static class LocId {

       private Long locId;
       private Long typeId;
       private String name;
       private Long parentId;
       private GeoCode geoCode;

       // +getters+setters

       @Override
       public String toString() {
           return "{\"locId\":" + locId
                   + ", \"typeId\":" + typeId 
                   + ", \"name\":" + name
                   + ", \"geoCode\":" + geoCode.toString() + "}";
       }

   }

   public static class GeoCode {

       private String type;
       private Coordinates coordinates;

       // +getter+setter

       @Override
       public String toString() {
           //return "{\"type\":" + type + "}";
           return "{\"type\":" + type
                   + ", \"coordinates\":" + coordinates.toString() + "}";
       }

   }

   public static class Coordinates {

       private Double latitude;
       private Double longitude;

       // +getter+setter

       @Override
       public String toString() {
           return "[{\"latitude\":" + latitude
                   + ", \"longitude\":" + longitude + "}]";
       }

   }

}

To test that everything works I read in the JSON object as a string like this,

String str = "the JSON string shown above";

InputStream is = new ByteArrayInputStream(str.getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(is));

Location_JSON locations = new Gson().fromJson(br, Location_JSON.class);

System.out.println(locations.toString());

This produces a NullPointerException!

I implemented two of the Deserializer solutions found in this SO post, Get nested JSON object with GSON using retrofit but it still created the same null error.

According to this SO post, Java - Gson parsing nested within nested what I have should be close.

I tested my code without the nested objects i.e., I erased the nested objects from both the string and the Location_JSON container, and everything worked. So I believe this is a JSON nested object problem.

UPDATE:

If you're looking at this post I just want to point out that I accepted chengpohi's answer because it solved my initial question and chengpohi was the first to provide an answer. I did however have a second problem that I did not discover until after this question was solved. Sachin Gupta provided a working solution to my second problem. If you're using this post please check out BOTH answers down below. Thank you.

7
  • How do you think the JSON you've provided should be mapped to an instance of the POJO type you've defined? Commented Jun 9, 2015 at 3:10
  • This is my first time using JSON so I'm not really sure what that means. Commented Jun 9, 2015 at 3:11
  • Ignore the posts for a second. You have a JSON object, with a complex data structure. You have a POJO type with a complex data structures. Which parts of the JSON map to which parts of the Java object? Commented Jun 9, 2015 at 3:11
  • I thought it worked by matching the key from the key:value pair to the variable name. So if I have a variable named foo and a key value pair of foo:12 then my variable foo would get the value 12. Commented Jun 9, 2015 at 3:13
  • Good. So your JSON, at the root, is a JSON object with properties with keys locId, name, typeId, etc. You're using Gson with new Gson().fromJson(br, Location_JSON.class);, mapping that whole structure to a Location_JSON instance. Where are those properties in that class? Commented Jun 9, 2015 at 3:15

2 Answers 2

1
Location_JSON locations = new Gson().fromJson(br, Location_JSON.class);

it should be:

LocId locations = new Gson().fromJson(br, LocId.class);

You get NullPointerException, because your LocId have not be initiliazed. Your JSON is a object of LocId.

and your JSON:

"coordinates":
    [{
       "latitude":99.0,
       "longitude":99.0
    }]

should be:

"coordinates":
    {
       "latitude":99.0,
       "longitude":99.0
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I think that worked....but now it's saying "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 97 path $.geoCode.coordinates"
Ugh! This is an API response I don't have the ability to modify the JSON.
I think I can use this SO post to fix the rest of the problem stackoverflow.com/questions/27628096/json-array-to-java-objects.
1

As already stated in above answer, you have to use LocId class as primary one.

now for java.lang.IllegalStateException you can modify GeoCode class to use array of Coordinates class. like :

public static class GeoCode {    
private String type;
private Coordinates []coordinates;

// +getter+setter

@Override
public String toString() {
return "GeoCode [type=" + type + ", coordinates=" + Arrays.toString(coordinates) + "]";
 }
}

1 Comment

Your answer helped me tremendously with the IllegalStateException. I wish I could accept two answers...

Your Answer

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.