一个树状json(quickBI的几个返回拼凑了一下,为了呈现一个类型较全的例子),实际使用中这种json里面有bool、string、int,再来个list套map的情况不少见
{
"Id":"abcdefg",
"sucess":true,
"index":12,
"parent":{
"usergroupId":"123456",
"usergroupName":"所有组",
"usergroupDesc":"所有成员",
},
"children":[
{
"parent":{
"usergroupId":"789",
"usergroupName":"分组1",
"usergroupDesc":""
},
"children":[]
}
]
}
// 先把json字符串转成JSONObject类型
JSONObject jsonObject = JSONObject.parseObject(json);
// 提取String用getString
String Id = jsonObject.getString("Id");
// 提取Boolean用getBoolean
Boolean success = jsonObject.getBoolean("success");
// 提取Integer用getInteger
Integer index = jsonObject.getInteger("index");
// 提取其他类型用get
Map<String, String> parent = (Map<String, String>) jsonObject.get("parent");
// children是JSONArray类型
JSONArray jsonchildren = (JSONArray) jsonObject.get("children");
List<Map<String, String>> childrens = JSONObject.parseObject(jsonchildren.toString(),
new TypeReference<List<Map<String, String>>>() {});