0

following is the screenshot of http respone in java -

screenshot of http response

and following is the text form of response:

{
  "LightGingerTheTextResult": [
    {
      "Confidence": 4,
      "From": 0,
      "LrnFrg": null,
      "LrnFrgOrigIndxs": [],
      "Mistakes": [
        {
          "CanAddToDict": false,
          "From": 0,
          "To": 0
        }
      ],
      "ShouldReplace": true,
      "Suggestions": [
        {
          "LrnCatId": 12,
          "Text": "An"
        },
        {
          "LrnCatId": 45,
          "Text": "A"
        }
      ],
      "To": 0,
      "TopLrnCatId": 12,
      "Type": 3,
      "UXFrgFrom": 0,
      "UXFrgTo": 6
    }
  ]
}

I want to extract the "text" in the suggestion.

This is my part with json. I am getting final response in "finalResult"-

JSONObject json = new JSONObject();
        try
        {
            StringBuffer response =urllib.urlopen(url);
            String finalResponse= response.toString();
            System.out.println("final response"+finalResponse);
            StringBuffer result=(StringBuffer) json.get(finalResponse);
            //finalResult=URLEncoder.encode(result.toString(), "UTF-8");
            String finalResult=result.toString();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
first
  • 37
  • 7
  • Use some object mapping to map the json to a POJO and then simply access the `text` property of that. – luk2302 May 01 '18 at 17:03
  • 1
    Or use a JSON parser like [JSON-P](https://javaee.github.io/jsonp/). –  May 01 '18 at 17:05
  • Show us what you have tried to solve this problem. Does it work? If not, what does not work? –  May 01 '18 at 17:08
  • The response is JSON text (as you know since you tagged it so), so use a JSON library to process it. – Andreas May 01 '18 at 17:12
  • Accept response as a string and use jsonpath library. See this https://stackoverflow.com/a/34117168/3295987. But its better to map json with POJO as you might need other fields as well (if not now, then in future) – Hemant Patel May 01 '18 at 17:19

3 Answers3

0

If you are looking for a value of a specific JSON node you can use a JsonPath expression e.g. to extract values of all Text nodes:

$.LightGingerTheTextResult[*].Suggestions[*].Text

in your example simplifies to

$..Text

or just the first Text node from the first Suggestions node:

$.LightGingerTheTextResult[0].Suggestions[0].Text
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
0

I would suggest you to first start by retreive the body of your httpResponse object.

 String tmp = response.body();   // I assume the callback method has a an 
                                 //argument of type 
                                 //httpResponse called response

Then store it somewhere eg:string.

Use gson and use the httpResponse class

like this: httpResponse rep = gson.fromJson(, httpResponse .class); This way you can now use the rep object to retreive what ever you want.

Nesan Mano
  • 1,892
  • 2
  • 26
  • 43
0

See stackoverflow.com/questions/2591098. You needs a library, using package org.json with

new JSONObject(textOfResponse)
.getJSONArray("LightGingerTheTextResult").getJSONObject(0)
.getJSONArray("Suggestions").getJSONObject(0)
.getString("Text")

and your textOfResponse I get

An
Giacomo
  • 94
  • 6