0

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. 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

I have JSON that looks something like this:

{  "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":

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:

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.

5
  • did you see this question:stackoverflow.com/questions/2864370/… Commented Mar 10, 2017 at 10:50
  • you can use this > Response response = new Gson().fromJson(s.toString(), Entry.class); Commented Mar 10, 2017 at 10:54
  • But if you can show json data completely it would be good to help. Commented Mar 10, 2017 at 10:54
  • @Milaci Yes, I saw that. When I write Entry[] entry = gson.fromJson(textTable, Entry[].class); I get Attempt to invoke virtual method 'java.lang.Object com.google.gson.Gson.fromJson(java.lang.String, java.lang.Class)' on a null object reference. Commented Mar 10, 2017 at 13:24
  • @DevSabby here is the gist with JSON. gist.github.com/deedora/98a8aadb08312a46f4627e6754339033 Unfortunately the first answer doesn't help me. Commented Mar 10, 2017 at 13:31

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.