复习解析嵌套json
在网络上找的一些经典的例子复习使用
一,解析嵌套json字符串,一个json字符串中嵌套另一个json字符串
{ "msg": { "resCode": "0000", "attrName": "sensorData", "result": { "food1": 0, "water": 0, "food2": 106, "humidity": "152.42", "temprature": "103.65" } } }
我要获取的是嵌套在msg里的result中的所有数据。
先用msg和result分别创建bean类,把Result作为msg的其中一个变量
public class SensorBean implements Serializable { private Result result; private int resCode; private String attrName; public Result getResult() { return result; } public void setResult(Result result) { this.result = result; } public int getResCode() { return resCode; } public void setResCode(int resCode) { this.resCode = resCode; } public String getAttrName() { return attrName; } public void setAttrName(String attrName) { this.attrName = attrName; } @Override public String toString() { return "Msg{" + "result=" + result.toString() + ", resCode=" + resCode + ", attrName='" + attrName + '\'' + '}'; } }
两个类都继承序列化
public class Result implements Serializable{ /** * food1 : 0 * water : 0 * food2 : 106 * humidity : 152.42 * temprature : 103.65 */ private int food1; private int water; private int food2; private String humidity; private String temprature; public int getFood1() { return food1; } public void setFood1(int food1) { this.food1 = food1; } public int getWater() { return water; } public void setWater(int water) { this.water = water; } public String getTemprature() { return temprature; } public void setTemprature(String temprature) { this.temprature = temprature; } @Override public String toString() { return "Result{" + "food1=" + food1 + ", water='" + water + ", temprature=" + temprature+ '\''+ '}'; } }
然后在获取到数据的地方进行Gson解析
Gson gson = new Gson();
SensorBean sensorBean = gson.fromJson(data.getString("msg"), SensorBean.class);
Result result = sensorBean.getResult();
L.d(TAG, "result.bean = " + result.toString());
二.加上数组的json嵌套
{ "error": "0", "data": [ { "name": "1", "value": [ { "name": "2", "value": "3" }, { "name": "4", "value": "5" } ] }, { "name": "6", "value": [ { "name": "7", "value": "8" }, { "name": "9", "value": "10" } ] } ] }
import java.util.ArrayList; public class JsonBean { private Error error; //data 是个数组 private ArrayList data; public Error getError(){ return error; } public void setError(Error error){ this.error = error ; } public ArrayList getData(){ return data; } public void setData(ArrayList data){ this.data = data; } public static class Bean2 { public String name; public ArrayList value; public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList getValue() { return value; } public void setValue(ArrayList value) { this.value = value; } @Override public String toString() { return "Bean2 [name=" + name + ", value=" + value + "]"; } } public static class Bean3 { public String name; private String value; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "Bean3 [name=" + name + ", value=" + value + "]"; } } } }