简体   繁体   English

在Android中反序列化JSON嵌套对象

[英]Deserialize json nested object in android

I am in trouble. 我有麻烦了。 I can not deserialize this object that I return json from an http request. 我无法反序列化从http请求返回json的对象。 Can anyone help me? 谁能帮我? I downloaded and added to the libs folder gson_2.2.4.jar . 我下载并添加到libs文件夹gson_2.2.4.jar We insert the object json 我们插入对象json

{
    "returnCode": 0,
    "data": [
        {
            "token": "aaaaa =",
            "code": "xx",
            "id": ""
        }
    ],
    "errorMsg": ""
}

You need to create a class of data object, for example 您需要创建一个数据对象类,例如

public class DataObj {
    public String token;
    public String code;
    public String id;
}

and then create another class for the whole json, for example 然后为整个json创建另一个类,例如

public class MyObj {
    public int returnCode;
    public DataObj[] data;
    public String errorMsg;
}

then create an object of MyObj and use deserializer from GSON to read json, for example: 然后创建一个MyObj对象,并使用GSON的反序列化器读取json,例如:

GSON gson = new GSON();
MyObj newMyObj = gson.fromJson(jsonString, MyObj.class);

Where jsonString contains the json object as string. 其中jsonString包含json对象作为字符串。

(@Shivam Verma thanks for your edit) (@Shivam Verma感谢您的编辑)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM