获取json的二级json
{
"code": "200",
"data": {
"token": "123456"
},
"message": "success"
}
public static JSONObject
getJson() {
JSONObject result
= new JSONObject();
JSONObject jsonObject
= new JSONObject();
jsonObject
.put("token", "123456");
result
.put("data", jsonObject
);
result
.put("code", "200");
result
.put("message", "success");
return result
;
}
public static void main(String
[] args
) {
JSONObject result
= getJson();
System
.out
.println(result
);
JSONObject recordMap
= result
.getJSONObject("data");
String token
= recordMap
.getString("token");
System
.out
.println(token
);
}
data里面为数组类型:
{
"code": "200",
"data": [
{
"address": "东方大道01号",
"clientId": "487009",
"clientName": "张三"
},
{
"address": "东方大道02号",
"clientId": "487010",
"clientName": "张三"
},
{
"address": "东方大道03号",
"clientId": "487011",
"clientName": "张三"
},
{
"address": "东方大道04号",
"clientId": "487012",
"clientName": "张三"
}
],
"message": "success"
}
public static JSONObject
getUserInfo() {
JSONObject result
= new JSONObject();
String object
="[{\"address\":\"东方大道01号\",\"clientId\":487009,\"clientName\":\"张三\"},{\"address\":\"东方大道02号\",\"clientId\":487010,\"clientName\":\"张三\"},{\"address\":\"东方大道03号\",\"clientId\":487011,\"clientName\":\"张三\"},{\"address\":\"东方大道04号\",\"clientId\":487012,\"clientName\":\"张三\"}]";
result
.put("data", object
);
result
.put("code", "200");
result
.put("message", "success");
return result
;
}
public static void main(String
[] args
) {
JSONObject result
= getUserInfo();
System
.out
.println(result
);
String data
= result
.getString("data");
System
.out
.println(data
);
List
<User> object
= (List
<User>) JSONArray
.parseArray(data
, User
.class);
System
.out
.println(object
);
}
转载请注明原文地址:https://tech.qufami.com/read-7881.html