使json从没有索引的数组中变为pojo

时间:2018-04-25 04:05:48

标签: android json retrofit pojo jsonschema2pojo

我正在使用改装,我有一个这样的联系人类:

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

}

我的json有一部分:

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

我正在使用改造来获取此JSON

问题是如何通过android.I中的pojo来管理它 我不想得到数据&#39;部分只是回应&#39;和&#39;消息&#39;

我的应用程序的改造部分非常好并且正在运行

3 个答案:

答案 0 :(得分:1)

您需要像这样进行JSON解析

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

答案 1 :(得分:1)

创建Serializable类 -

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
}

现在在Gson的帮助下解析JSON数据。

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

在上面的POJO课程中,您可以根据需要添加其他数据。为了避免任何特定属性进行序列化和反序列化,您可以使用 排除策略

有关此内容的更多信息,请访问here

答案 2 :(得分:0)

如果您想避免在关键字下方使用任何数据 的瞬态

并根据上面给出的json pojo类将是这样的..

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

}

然后最后回应pojo课..

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

}