我需要一种无需反序列化即可解析json响应的方法。请在下面找到我的示例json响应:需要从results-> data-> categories获取值。不确定如何遍历类别值。提前致谢。请在下面找到我的示例json响应和示例代码:
{
"results": {
"data": {
"columns": [{
"data_type": 0,
"key": "",
"name": "",
"sort_key": ""
}],
"categories": [{
"data_type": 0,
"key": "",
"name": "",
"sort_key": ""
},
{
"data_type": 0,
"key": "",
"name": "",
"sort_key": ""
}
],
"properties": {
"container": false,
"container_size": 0,
"create_date": "",
"create_user_id": 0,
"description": "",
"description_multilingual": {
"en": "",
"de": ""
},
"external_create_date": "date",
"external_identity": "",
"external_identity_type": "",
"external_modify_date": "date",
"external_source": "",
"favorite": false,
"guid": "",
"icon": "",
"icon_large": "",
"id": 0,
"modify_date": "date",
"modify_user_id": 0,
"name": "",
"name_multilingual": {
"en": "",
"de": ""
},
"owner": "",
"owner_group_id": 0,
"owner_user_id": 0,
"parent_id": 0,
"reserved": false,
"reserved_date": "",
"reserved_user_id": 0,
"type": 0,
"type_name": "",
"versions_control_advanced": false,
"volume_id": 0
}
}
},
"links": {
"data": {
"self": {
"body": "",
"content_type": "",
"href": "",
"method": "",
"name": ""
}
}
}
}
请在下面找到我使用的示例代码:
ResponseEntity<LinkedHashMap<String, Object>> resp = restTemplate.exchange(url,HttpMethod.Get, entity,typeref);
resp.getBody().forEach((key, value)) ->{
if(key.equalsIgnoreCase("results")){
LinkedHashMap<String, Object> results = (LinkedHashMap<String, Object>)value;
LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>)results.get("data");
LinkedHashMap<String, Object> categories= (LinkedHashMap<String, Object>)data.get("categories");
//read category properties here
}
});
答案 0 :(得分:-1)
this is more simple example for json parse in java
在这里您可以做类似的事情:
//response is the string contains the json
JSONObject mainObj = new JSONObject(response);
//now we have the main json object
String cat = mainObj.getJSONObject("results").getJSONObject("data").getString("categories");
//now we have the string contains the catigories
JSONObject categories = new JSONObject(cat);