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.

Community
  • 1
  • 1
Kyle Bridenstine
  • 6,055
  • 11
  • 62
  • 100
  • How do you think the JSON you've provided should be mapped to an instance of the POJO type you've defined? – Sotirios Delimanolis Jun 09 '15 at 03:10
  • This is my first time using JSON so I'm not really sure what that means. – Kyle Bridenstine Jun 09 '15 at 03: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? – Sotirios Delimanolis Jun 09 '15 at 03: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. – Kyle Bridenstine Jun 09 '15 at 03: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? – Sotirios Delimanolis Jun 09 '15 at 03:15
  • I just saw you edit this http://stackoverflow.com/a/27633777/3299397 :) – Kyle Bridenstine Jun 09 '15 at 03:34
  • Ok I see what you're saying now. I need to provide the object that houses the initial properties/keys. That makes sense. – Kyle Bridenstine Jun 09 '15 at 03:38

2 Answers2

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
    }
chengpohi
  • 14,064
  • 1
  • 24
  • 42
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) + "]";
 }
}
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45