简体   繁体   English

JSON在Android中将子树提取为字符串

[英]JSON extract subtree as string in android

I have the following json structure: 我有以下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"
    },

How do I extract the entire node "Clauses" and convert it as string? 如何提取整个节点“ Clauses”并将其转换为字符串? The output should be: 输出应为:

"Clauses": {
                "MustClauses": [
                    {
                        "Match": {
                            "Attribute": "Channel",
                            "Operator": "None",
                            "Value": "n twenty four c h"
                        }
                    }
                ],
                "MustNotClauses": []
            },

An optimal solution is needed. 需要最佳解决方案。

For simple parsing I use JsonReader. 对于简单的解析,我使用JsonReader。

Let's say you have JSONObject with name interpretation, then following is the simple way to get Clauses: 假设您有带有名称解释的JSONObject,那么以下是获取Clauses的简单方法:

JSONObject complexSearchRequest = interpretation.getJSONObject("ComplexSearchRequest");
JSONObject clauses = complexSearchRequest.getJSONObject("Clauses");
Log.d("clauses",clauses.toString());

Try using org.json. 尝试使用org.json。 It's the best I've seen: https://mvnrepository.com/artifact/org.json/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"));

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM