我有以下json结构:
"interpretation": {
"ComplexSearchRequest": {
"BoostLanguage": 0,
"Clauses": {
"MustClauses": [
{
"Match": {
"Attribute": "Channel",
"Operator": "None",
"Value": "n twenty four c h"
}
}
],
"MustNotClauses": []
},
"FilterLanguages": [],
"ItemFilters": [],
"MaxResults": 10
},
"GraphManagerRequestId": "baf28b69-0806-4725-9fb4-1d2acd1944ea",
"GraphManagerRequestLanguage": "de",
"Type": "Search"
},
如何提取整个节点“子句”并将其转换为字符串?输出应为:
"Clauses": {
"MustClauses": [
{
"Match": {
"Attribute": "Channel",
"Operator": "None",
"Value": "n twenty four c h"
}
}
],
"MustNotClauses": []
},
需要一个最佳解决方案。
对于简单的解析,我使用JsonReader。
答案 0 :(得分:1)
假设你有一个带有名称解释的JSONObject,那么以下是获取子句的简单方法:
JSONObject complexSearchRequest = interpretation.getJSONObject("ComplexSearchRequest");
JSONObject clauses = complexSearchRequest.getJSONObject("Clauses");
Log.d("clauses",clauses.toString());
答案 1 :(得分:1)
尝试使用org.json。这是我见过的最好的:https://mvnrepository.com/artifact/org.json/json
import org.json.JSONException;
import org.json.JSONObject;
public class JsonSample
{
public static void main(String[] args) throws JSONException
{
String json = "{ \"interpretation\": { \"ComplexSearchRequest\": { \"BoostLanguage\": 0, \"Clauses\": { \"MustClauses\": [ { \"Match\": { \"Attribute\": \"Channel\", \"Operator\": \"None\", \"Value\": \"n twenty four c h\" } } ], \"MustNotClauses\": [] }, \"FilterLanguages\": [], \"ItemFilters\": [], \"MaxResults\": 10 }, \"GraphManagerRequestId\": \"baf28b69-0806-4725-9fb4-1d2acd1944ea\", \"GraphManagerRequestLanguage\": \"de\", \"Type\": \"Search\" }}";
JSONObject jsonObject = new JSONObject(json);
System.out.println(jsonObject.getJSONObject("interpretation").getJSONObject("ComplexSearchRequest").getString("Clauses"));
}
}