JSON 嵌套对象

JSON Nested Objects

我正在处理一个 JSON 文件,其中包含这样的嵌套对象,

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

我创建了一个容器来将 JSON 文件保存在 class 中,

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

   }

}

为了测试一切正常,我在 JSON 对象中读取了这样一个字符串,

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());

这会产生 NullPointerException!

我实现了这个 SO post 中的两个解串器解决方案, Get nested JSON object with GSON using retrofit 但它仍然产生相同的空错误。

根据这个 SO post, Java - Gson parsing nested within nested我有的应该差不多了。

我在没有嵌套对象的情况下测试了我的代码,即我从字符串和 Location_JSON 容器中删除了嵌套对象,一切正常。所以我认为这是一个 JSON 嵌套对象问题。

更新:

如果你正在看这个 post 我只想指出我接受了 chengpohi 的回答因为它解决了我最初的问题而 chengpohi 是先提供一个答案。然而,我确实遇到了第二个问题,直到这个问题得到解决后我才发现。 Sachin Gupta 为我的第二个问题提供了可行的解决方案。如果您正在使用此 post,请查看下面的两个答案。谢谢。

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

应该是:

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

你得到 NullPointerException,因为你的 LocId 还没有初始化。您的 JSON 是 LocId 的对象。

和你的 JSON:

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

应该是:

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

如上回答所述,您必须使用 LocId class 作为主要答案。

现在对于 java.lang.IllegalStateException,您可以修改 GeoCode class 以使用 Coordinates class 的数组。喜欢 :

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

// +getter+setter

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