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

问题描述:

这是一连串的反序列化问题,但我已经阅读了所有这些问题,无法找出解决方案。我需要获取所有的“entery” - >“content” - > $ t和“entery” - >“title” - >“$ t”,但是在CategoryDe​​serializer()中,我得到的JsonArray是NULL。我已经指出“< ====”代码的一部分。使用GSON反序列化对象的JSON数组

错误消息:

尝试上的空对象引用调用接口方法 '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;} 

我的Entry类:

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; 
    } 

} 

编辑:我已经声明和实例化。

+0

你看到了这个问题:HTTP://*.com/questions/2864370/how-do-i-use-googles-gson-api-to-deserialize- json-properly – Milaci

+0

你可以使用this> Response response = new Gson()。fromJson(s.toString(),Entry.class); –

+0

但是,如果您可以完全显示json数据,那将会很有帮助。 –

我很复杂的事情并不复杂。所有我需要做的就是这样的:

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