简体   繁体   English

JSON嵌套对象

[英]JSON Nested Objects

I'm working with a JSON file that has nested objects like this, 我正在使用具有这样的嵌套对象的JSON文件,

{
  "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, 我创建了一个容器来将JSON文件保存在这样的类中,

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, 为了测试一切正常,我将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());

This produces a NullPointerException! 这将产生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. 我实现了这篇SO帖子中找到的两个Deserializer解决方案, 使用翻新功能通过GSON获取嵌套的JSON对象,但仍然会产生相同的null错误。

According to this SO post, Java - Gson parsing nested within nested what I have should be close. 根据这则SO帖子, 嵌套在Java-Gson中的嵌套我应该关闭的东西。

I tested my code without the nested objects ie, I erased the nested objects from both the string and the Location_JSON container, and everything worked. 我测试了没有嵌套对象的代码,即,我从字符串和Location_JSON容器中删除了嵌套对象,一切正常。 So I believe this is a JSON nested object problem. 因此,我相信这是一个JSON嵌套对象问题。

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. 如果您正在看这篇文章,我只想指出我接受chengpohi的答案,因为它解决了我的最初问题,而chengpohi是第一个提供答案的人。 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. Sachin Gupta为我的第二个问题提供了可行的解决方案。 If you're using this post please check out BOTH answers down below. 如果您使用的是这篇文章,请在下面查看两个答案。 Thank you. 谢谢。

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. 你得到NullPointerException ,因为你的LocId没有被initiliazed。 Your JSON is a object of LocId . 您的JSON是LocId的对象。

and your JSON: 和您的JSON:

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

should be: 应该:

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

As already stated in above answer, you have to use LocId class as primary one. 正如上面的答案中已经提到的那样,您必须使用LocId类作为主要类。

now for java.lang.IllegalStateException you can modify GeoCode class to use array of Coordinates class. 现在,对于java.lang.IllegalStateException您可以修改GeoCode类以使用Coordinates类的数组。 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) + "]";
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM