java - 使用 GSON 反序列化 JSON 对象数组

标签 java json gson deserialization json-deserialization

这是一系列反序列化问题中的一个,但我已经阅读了所有这些问题,但无法找到解决我的问题的方法。

我需要获取所有“entery”->“content”->$t 和“entery”->“title”->“$t”,但在 CategoryDe​​serializer() 中我得到的 JsonArray 为 NULL。我已经用“<====”指出了这部分代码。

错误消息:

Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference

我的 JSON 看起来像这样:

{  "feed":{  
      "id":{ ... },
      "author":[ ... ],
      "entry":[  
         {  
            "id":{  },
            "updated":{  },
            "category":[  ],
            "title":{ 
                "type":"text",
                "$t":"A1 },
            "content":{  
               "type":"text",
               "$t":"test"
            },
            "link":[  ]
         },
         { ... },
         { ... },
         {  ...},
      ]
   }
}

这是我尝试反序列化“entery”的代码的一部分:

String json = response.body().toString();

Type listType = new TypeToken<List<Entry>>() {
                            }.getType();

Gson gson = new GsonBuilder().registerTypeAdapter(listType, new CategoryDeserializer()).create();

List<Entry> list = gson.fromJson(json, listType);

for (Entry entry : list) {
    Log.i("MainActivity", "Content: " + entry.getContent());}

其中 CategoryDe​​serializer() 如下所示:

public class CategoryDeserializer implements JsonDeserializer<List<Entry>> {
public List<Entry> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {

      JsonArray entry = je.getAsJsonObject().getAsJsonArray("entry");  //<==== here I get that entry is null but je has a value
      ArrayList<Entry> myList = new ArrayList<Entry>();

      for (JsonElement e : entry) {
           myList.add((Entry) jdc.deserialize(e, Entry.class));
      }

    return myList;}

还有我的入门类(class):

public class Entry {

    private Id_ id;

    private Updated_ updated;

    private List<Category_> category = null;

    private Title_ title;

    private Content content;

    private List<Link_> link = null;

    //getters and setters

    public Id_ getId() {
        return id;
    }

    public void setId(Id_ id) {
        this.id = id;
    }

    public Updated_ getUpdated() {
        return updated;
    }

    public void setUpdated(Updated_ updated) {
        this.updated = updated;
    }

    public List<Category_> getCategory() {
        return category;
    }

    public void setCategory(List<Category_> category) {
        this.category = category;
    }

    public Title_ getTitle() {
        return title;
    }

    public void setTitle(Title_ title) {
        this.title = title;
    }

    public Content getContent() {
        return content;
    }

    public void setContent(Content content) {
        this.content = content;
    }

    public List<Link_> getLink() {
        return link;
    }

    public void setLink(List<Link_> link) {
        this.link = link;
    }

}

编辑:我有声明和实例化。

最佳答案

我把原本并不复杂的事情复杂化了。我所要做的就是:

String json = response.body().toString();
Gson mGson = new Gson();

//where Example is the root of JSON
Example rsp = mGson.fromJson(json, Example.class);

//Entry is the list I needed to access
List <Entry> listOfEntrys= rsp.getFeed().getEntry();

//get value
Log.i("MainActivity", "listaEntrya " + listOfEntrys.get(0).getTitle().get$t());

关于java - 使用 GSON 反序列化 JSON 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42706435/

相关文章:

使用Gson对泛型进行Java反序列化

Android GSON反序列化删除空数组

java - 使 3 个 View 可见,然后根据条件隐藏其他 View

java - 如何每5分钟调用一个函数?

javascript - 为什么javascript函数没有运行?

javascript - 通过 fetch() 调用 API - 输入意外结束

android - Kotlin 字段覆盖和 GSON 反序列化 Unable to create converter class 异常

java - 在 Java 中使用枚举创建构造函数

Java Base64 MIME 解码/编码会丢弃分隔符

php - 从服务器 json 响应延迟加载 ListView 项目