1

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.

3

2 Answers 2

2

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"));

  }

}
Sign up to request clarification or add additional context in comments.

Comments

2

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.