make json to pojo from an array without index

心已入冬 提交于 2019-12-11 14:54:39

问题


I'm using retrofit and I have a contact class like this :

public class MyContact {

    private String response;
    private String message;

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

and there is a part of my json :

{"Response":"Success","Message":"YES","Data":{"Info":{"id":"1" , "name":"leon"}}}

I'm using retrofit to get this JSON

The question is how to manage it by pojo in android.I I don't want to get 'Data' part just 'response' and 'message'

My retrofit part of the app is completely nice and running


回答1:


You will need to do JSON Parsing like this

String res = "{\"Response\":\"Success\",\"Message\":\"YES\",\"Data\":{\"Info\":{\"id\":\"1\" , \"name\":\"leon\"}}}";

MyContact model =new MyContact();

if (res!=null);//Retrofit response 
    try
    {
        JSONObject jo = new JSONObject(res);
        model.setResponse(jo.optString("Response"));
        model.setMessage(jo.optString("Message"));

        Log.d("Akshay","Response = "+model.getResponse() + " " +model.getMessage());
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }



回答2:


Create the Serializable class -

class Response implements Serializable{
    @SerializedName("Response")
    private String response;
    @SerializedName("Message")
    private String message;

    public Response(){}

    public Response(String response, String message){
        this.message = message;
        this.response = response;
    }

    //todo getter and setter methods
}

Now parse the JSON data with help of Gson.

String jsonString = "{\"Response\":\"Success\",\"Message\":\"YES\",\"Data\":{\"Info\":{\"id\":\"1\" , \"name":\"leon\"}}}";
Response responseObject = new Gson()
                          .fromJson(
                             jsonString,
                             Response.class
                          );

in this POJO class above you can add other data if you want. for avoiding any specific property to serialize and deserialize you can use exclusion strategy.

For more information on this yoou can go here.




回答3:


If you want to avoid any data used below keyword transient

and according to given above json pojo class will be like this..

public class Data {

@SerializedName("Info")
private Info info;

public Info getInfo() {
    return info;
}

public void setInfo(Info info) {
    this.info = info;
}

}

public class Info {

@SerializedName("id")
private String id;
@SerializedName("name")
private String name;

public String getId() {
    return id;
}

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

public String getName() {
    return name;
}

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

}

then finaly response pojo class..

public class ResponseData {

@SerializedName("Response")
private String response;
@SerializedName("Message")
private String message;
@SerializedName("Data")
transient 
private Data data;

public String getResponse() {
    return response;
}

public void setResponse(String response) {
    this.response = response;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}

}



来源:https://stackoverflow.com/questions/50013890/make-json-to-pojo-from-an-array-without-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!