简体   繁体   中英

JSON extract subtree as string in android

I have the following json structure:

 "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? 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.

Let's say you have JSONObject with name interpretation, then following is the simple way to get Clauses:

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

Try using org.json. It's the best I've seen: 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"));

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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