簡體   English   中英

使用GSON反序列化對象的JSON數組

[英]Deserializing JSON array of objects using GSON

這是反序列化問題中的一連串問題,但是我已經閱讀了所有問題,無法解決我的問題的解決方案。

我需要獲取所有的“ entery”->“ content”-> $ t和“ entery”->“ title”->“ $ t”,但是在CategoryDe​​serializer()中,我得到的JsonArray為NULL。 我已經用“ <====“指向了代碼的這一部分。

錯誤信息:

嘗試在空對象引用上調用接口方法'java.util.Iterator java.util.List.iterator()'

我有看起來像這樣的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;}

我的入門班:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM