Android 如何使用Gson解析JSON对象中的多个JSON数组?

Android 如何使用Gson解析JSON对象中的多个JSON数组?,android,arrays,parsing,gson,Android,Arrays,Parsing,Gson,如何使用Gson解析JSON对象中的多个JSON数组 { "id": 1, "Data": { "Details": [{ "Code": "1", "Name": "John" }, { "Code": "2", "Name": "Peter" }], "Other": [{ "age": "56

如何使用Gson解析JSON对象中的多个JSON数组

{
    "id": 1,
    "Data": {
        "Details": [{
            "Code": "1",
            "Name": "John"
        }, {
            "Code": "2",
            "Name": "Peter"
        }],
        "Other": [{
            "age": "56",
            "gender": "M"
        }, {
            "age": "66",
            "gender": "M"
        }]
    },
    "message": "SUCCESS"
}
任何帮助都将不胜感激。

简单

JSONObject jsonObj = new JSONObject(yourStringHere).optJSONObject("Data");
JSONArray jsonDetail = jsonObj.optJSONArray("Details");
JSONArray jsonOther = jsonObj.optJSONArray("Other");

您可以使POJO类传入Gson进行JSON解析

Gson-Gson=new-Gson();
Detail details=gson.fromJson(响应,Detail.class);
com.example.Data.java
package.com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类数据{
@序列化名称(“详细信息”)
@暴露
私有列表详细信息=null;
@序列化名称(“其他”)
@暴露
私有列表其他=空;
公共列表getDetails(){
退货详情;
}
公共无效设置详细信息(列表详细信息){
this.details=详细信息;
}
公共列表getOther(){
归还他人;
}
公共无效设置其他(列出其他){
this.other=其他;
}
}
com.example.Detail.java
package.com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类详细信息{
@序列化名称(“代码”)
@暴露
私有字符串码;
@序列化名称(“名称”)
@暴露
私有字符串名称;
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
com.example.example.java
package.com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课范例{
@序列化名称(“id”)
@暴露
私有整数id;
@SerializedName(“数据”)
@暴露
私人数据;
@SerializedName(“消息”)
@暴露
私有字符串消息;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共数据getData(){
返回数据;
}
公共无效设置数据(数据){
这个数据=数据;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
}
com.example.Other.java
package.com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类其他{
@序列化名称(“年龄”)
@暴露
私弦时代;
@序列化名称(“性别”)
@暴露
私人字符串性别;
公共字符串getAge(){
回归年龄;
}
公共无效设置(字符串期限){
这个。年龄=年龄;
}
公共字符串getGender(){
返回性别;
}
公共无效设置性别(字符串性别){
这个。性别=性别;
}
}

转到下面的链接并创建Pojo类

创建后,使用gson从json获取数据

YourPojoClass obj = new Gson().fromJson(jsonResponse, YourPojoClass.class);

尝试此操作并更新答案

创建简单的POJO类

public class JsonResponse{
    int id;
    DataResponse data;
    String message;

    //setter and getter
}

public class DataResponse{
     List<DetailsResponse> Details;
     List<OthersResponse> Other;

     //setter and getter
}

public class DetailsResponse{
     String Code;
     String Name;

     //setter and getter
}

public class OthersResponse{
     String age;
     String gender;

     //setter and getter
}
公共类JsonResponse{
int-id;
数据响应数据;
字符串消息;
//接二连三
}
公共类数据响应{
列出详情;
列出其他;
//接二连三
}
公共类详细信息响应{
字符串代码;
字符串名;
//接二连三
}
公共类其他响应{
弦年龄;
字符串性别;
//接二连三
}
最后,

JsonResponse data = new Gson().fromJson(YOUR_JSON,JsonResponse.class);
//how to use
int id = data.getId();
List<DetailsResponse> tt = data.getData().getDetails();
List<OthersResponse> to = data.getData().getOthers();
JsonResponse data=new Gson().fromJson(您的_JSON,JsonResponse.class);
//如何使用
int id=data.getId();
List tt=data.getData().getDetails();
List to=data.getData().getOthers();

从json创建模型,并使用GSON简单解析。使用此站点创建POJO类。这不是GSON。