0

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.

Duna
  • 1,564
  • 1
  • 16
  • 36
  • share the format of expected output after convert it as a string – GThamizh Jul 06 '17 at 07:53
  • May this link can help you : [Android JSON Parsing tutorial](http://www.technotalkative.com/android-json-parsing/) – Frank Jul 06 '17 at 08:04
  • try this, https://stackoverflow.com/questions/39389510/extract-specific-node-of-json-response-in-resteasy-client – GThamizh Jul 06 '17 at 08:07

2 Answers2

1

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());
Usman Rana
  • 2,067
  • 1
  • 21
  • 32
1

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

  }

}
Buffalo
  • 3,861
  • 8
  • 44
  • 69