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

时间:2017-02-16 12:53:24

标签: android arrays parsing gson

如何使用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"
}

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:1)

简单!

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

答案 1 :(得分:1)

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

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {

    @SerializedName("Details")
    @Expose
    private List<Detail> details = null;

    @SerializedName("Other")
    @Expose
    private List<Other> other = null;

    public List<Detail> getDetails() {
        return details;
    }

    public void setDetails(List<Detail> details) {
        this.details = details;
    }

    public List<Other> getOther() {
        return other;
    }

    public void setOther(List<Other> other) {
        this.other = other;
    }

}

com.example.Data.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Detail {

    @SerializedName("Code")
    @Expose
    private String code;

    @SerializedName("Name")
    @Expose
    private String name;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

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

}

com.example.Detail.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("Data")
    @Expose
    private Data data;

    @SerializedName("message")
    @Expose
    private String message;

    public Integer getId() {
        return id;
    }

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

    public Data getData() {
        return data;
    }

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

    public String getMessage() {
        return message;
    }

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

}

com.example.Example.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Other {

    @SerializedName("age")
    @Expose
    private String age;

    @SerializedName("gender")
    @Expose
    private String gender;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

com.example.Other.java

1.
Let's say 
I have a table Student with , StudentID INT, ClassID INT , Name VARCHAR().
2.
Here is the JavaScript Code
    function AddStudent() {
    var data = { "ClassID": 1, "Name": 'ABC' }

    $.ajax({
        url: '/Admin/AddNewStudent',
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        cache: false,
        data: JSON.stringify(data),
        success: function (ServiceResponce) {
             refreshgird();
        }
    });
}

    3.
    StudentController.cs

    public JsonResult AddNewStudent(Student obj)
            {
                try
                {
                    var add = db.AddNewStudent(obj.ClassID, obj.Name).ToString();
                    return Json(add, JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    return Json(ex.ToString(), JsonRequestBehavior.AllowGet);
                }
            }

4. Student.cs
 public class Student 
    {
        public int ClassID{ get; set; }

        public string Name{ get; set; }
    }

答案 2 :(得分:0)

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

http://pojo.sodhanalibrary.com/

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

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

试试这个并更新答案

答案 3 :(得分:0)

创建简单的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 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();