JSON Nested Objects

1.2k views Asked by At

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.

2

There are 2 answers

3
chengpohi On BEST ANSWER
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
    }
1
Sachin Gupta On

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) + "]";
 }
}