How parse json array with multiple objects by gson?

后端 未结 3 1256
陌清茗
陌清茗 2020-12-10 16:52

How can I parse json using gson? I have a json array with multiple object types, and I don\'t know, what kind of object I need to create to save this structure. I cannot cha

3条回答
  •  隐瞒了意图╮
    2020-12-10 17:11

    You can set the methods in your model class very easily. Just make a StringRequest. Below is a snippet:

    List inpList;
    StringRequest greq = new StringRequest(Request.Method.POST, yourURL, new Response.Listener() {
                @Override
                public void onResponse(String response) {
                    try {
                            Log.d("response array===>  ", response.toString());
    
                            Type type = new TypeToken>(){}.getType();
                            inpList = new Gson().fromJson(response, type);
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
    
                }
            }){
                @Override
                protected Map getParams() throws AuthFailureError {
                    Map params = new HashMap();
                    //return params back to server, if any
                }
            };
    
            yourVolley.getRequestQueue().add(greq);
    

    I have used this to generate your model class from you json. Your model class will look something like this:

     package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    
    @Generated("org.jsonschema2pojo")
    public class YourModelClass {
    
    @Expose
    private Integer type;
    @Expose
    private Object object;
    
    /**
    * 
    * @return
    * The type
    */
    public Integer getType() {
    return type;
    }
    
    /**
    * 
    * @param type
    * The type
    */
    public void setType(Integer type) {
    this.type = type;
    }
    
    /**
    * 
    * @return
    * The object
    */
    public Object getObject() {
    return object;
    }
    
    /**
    * 
    * @param object
    * The object
    */
    public void setObject(Object object) {
    this.object = object;
    }
    
    }
    -----------------------------------com.example.Object.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    
    @Generated("org.jsonschema2pojo")
    public class Object {
    
    @Expose
    private Integer id;
    @Expose
    private Integer type;
    @Expose
    private String city;
    
    /**
    * 
    * @return
    * The id
    */
    public Integer getId() {
    return id;
    }
    
    /**
    * 
    * @param id
    * The id
    */
    public void setId(Integer id) {
    this.id = id;
    }
    
    /**
    * 
    * @return
    * The type
    */
    public Integer getType() {
    return type;
    }
    
    /**
    * 
    * @param type
    * The type
    */
    public void setType(Integer type) {
    this.type = type;
    }
    
    /**
    * 
    * @return
    * The city
    */
    public String getCity() {
    return city;
    }
    
    /**
    * 
    * @param city
    * The city
    */
    public void setCity(String city) {
    this.city = city;
    }
    
    }
    

提交回复
热议问题