简体   繁体   English

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

[英]Deserializing JSON array of objects using GSON

This is one in a row of deserialization questions but I've read them all and can't figure out the solution for my problem. 这是反序列化问题中的一连串问题,但是我已经阅读了所有问题,无法解决我的问题的解决方案。

I need to get all the "entery"->"content" ->$t and "entery"->"title"->"$t" but in CategoryDeserializer() I get JsonArray that is NULL. 我需要获取所有的“ entery”->“ content”-> $ t和“ entery”->“ title”->“ $ t”,但是在CategoryDe​​serializer()中,我得到的JsonArray为NULL。 I've pointed to that part of the code with "<====". 我已经用“ <====“指向了代码的这一部分。

Error message: 错误信息:

Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference 尝试在空对象引用上调用接口方法'java.util.Iterator java.util.List.iterator()'

I have JSON that looks something like this: 我有看起来像这样的JSON:

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

This is part of my code where I try to deserialize "entery": 这是我尝试反序列化“ 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());}

Where CategoryDeserializer() looks like this: 其中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;}

And my Entry 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;
    }

}

Edit: I have declaration and instantiation. 编辑:我有声明和实例化。

I was complicating things that were not complicated. 我使事情复杂化了。 All I had to do was this: 我所要做的就是:

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

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

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