从没有索引的数组将json转换为pojo [英] make json to pojo from an array without index

查看:139
本文介绍了从没有索引的数组将json转换为pojo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用改造,并且有一个像这样的联系人类:

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

}

还有我的json的一部分:

and there is a part of my json :

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

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

I'm using retrofit to get this JSON

问题是如何通过android.I中的pojo对其进行管理 我不想只获得数据"部分的响应"和消息"

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

推荐答案

创建可序列化的类-

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类中,您可以根据需要添加其他数据.要避免序列化和反序列化的任何特定属性,可以使用 排除策略 .

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.

这篇关于从没有索引的数组将json转换为pojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆