本文介绍了如何使用Gson解析JSON对象内的多个JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Gson解析JSON对象中的多个JSON数组?
{
id:1 ,
Data:{
Details:[{
Code:1,
Name:John
},{
Code:2,
Name:Peter
}],
其他:[{
age:56 ,
性别:M
},{
年龄:66,
性别:M
}]
},
message:SUCCESS
}
任何帮助将不胜感激。
解决方案您可以让POJO类在Gson中传递以用于JSON解析
Gson gson = new Gson();
详细信息= gson.fromJson(response,Detail .class);
com.example.Data.java
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(其他)
@Expose
私人列表<其他> other = null;
公开列表< Detail> getDetails(){
返回细节;
}
public void setDetails(List< Detail> details){
this.details = details;
}
公开列表<其他> getOther(){
return other;
}
public void setOther(List< Other> other){
this.other = other;
$ b code
$ b com.example.Detail.java
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
公共类详细信息{
@SerializedName(Code)
@Expose
私有字符串代码;
@SerializedName(Name)
@Expose
私有字符串名称;
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.Example.java
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
$ b $ public class Example {
@SerializedName(id)
@Expose
private Integer id;
@SerializedName(Data)
@Expose
私有数据数据;
@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.Other.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(性别)
@Expose
私人字符串性别;
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;
}
}
How to parse multiple JSON arrays inside a JSON object using Gson?
{
"id": 1,
"Data": {
"Details": [{
"Code": "1",
"Name": "John"
}, {
"Code": "2",
"Name": "Peter"
}],
"Other": [{
"age": "56",
"gender": "M"
}, {
"age": "66",
"gender": "M"
}]
},
"message": "SUCCESS"
}
Any help would be appreciated.
解决方案
You can make POJO classes to pass in Gson for JSON parsing
Gson gson = new Gson();
Detail details = gson.fromJson(response, Detail .class);
com.example.Data.java
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.Detail.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.Example.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.Other.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;
}
}
这篇关于如何使用Gson解析JSON对象内的多个JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-21 18:16