FromJson Gson error

1

I am trying to download a JSON for later, using the GSON library, parsing it using the respective java class Answer.class: I try to download the JSON from the link: String link=" link "; I am using the following syntax in the activity.class after establishing the connection with loopj (and verify that it connects and downloads the string properly, I also save it in a variable "link")

Respuesta funt = gson.fromJson(json, Respuesta.class);

But it seems that I do not do it correctly because the application always stops in that line of code, how should I proceed?

I attach the Respusta.class

public class Respuesta {

private int id;
private String name;
private UserBean user;
private ItemsBean items;

public int getId() {return id;}

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

public String getName() {return name;}

public void setName(String name) {this.name = name;}

public UserBean getUser() {return user;}

public void setUser(UserBean user) {this.user = user;}

public ItemsBean getItems() {return items;}

public void setItems(ItemsBean items) {this.items = items;}

public static class UserBean {

    private String name;

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}
}

public static class ItemsBean {

    private HashMap<String, CodigoBean> codigo;

    public HashMap<String, CodigoBean> getCodigo(){return codigo;}

    public void setCodigo(HashMap<String, CodigoBean> codigo) {this.codigo = codigo;}

    public static class CodigoBean {

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {return id;}

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

        public int getStrong() {return strong;}

        public void setStrong(int strong) {this.strong = strong;}

        public boolean isActive() {return active;}

        public void setActive(boolean active) {this.active = active;}

        public String getSell() {return sell;}

        public void setSell(String sell) {this.sell = sell;}
    }
}}

Additional: I'm using HashMap<> to map the codes since it can be an indeterminate number of codes besides that I would not know what they are, is it okay to use HashMap<> that way?

Thanks in advance.

    
asked by LuDev 11.03.2018 в 02:36
source

1 answer

0

The structure you are trying to parser is from a Json Array, since it starts with "[" :

It is also important to map the fields of your class with those of the answer using the annotation SerializedName

import com.google.gson.annotations.SerializedName;

public class Respuesta{

@SerializedName("id")
private int id;
@SerializedName("name")
private String name;
@SerializedName("user")   
...
...

In this way you can get the values in an array:

 Gson gson = new Gson(); //Instancia Gson.
 Respuesta[] arrJson = gson.fromJson(json.toString(), Respuesta[].class);

Here is an example:

How can I create a class from a JSONObject with GSON (Volley Use)?

    
answered by 12.03.2018 в 17:30