从Http响应中提取文本

时间:2018-05-01 17:01:06

标签: java json httpresponse

以下是java中的http respone的屏幕截图 -

screenshot of http response

以下是回复的文本形式:

def loop_through(arg_name):
    for n in range(100):
        F(**{arg_name: n})

我想提取"文字"在建议中。

这是我与json的关系。我正在" finalResult" -

得到最终回复
{
  "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
    }
  ]
}

3 个答案:

答案 0 :(得分:0)

如果您要查找特定JSON节点的值,可以使用JsonPath expression,例如提取所有Text个节点的值:

$.LightGingerTheTextResult[*].Suggestions[*].Text
您的示例中的

简化为

$..Text

或仅是第一个Text节点中的第一个Suggestions节点:

$.LightGingerTheTextResult[0].Suggestions[0].Text

答案 1 :(得分:0)

我建议你首先通过retreive httpResponse对象的主体开始。

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

然后将其存储在某处,例如:string。

使用gson并使用httpResponse类

像这样: httpResponse rep = gson.fromJson(,httpResponse .class); 这样,您现在可以使用rep对象来检索您想要的内容。

答案 2 :(得分:0)

stackoverflow.com/questions/2591098。你需要一个图书馆, 使用包org.json

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

和你的textOfResponse

An
相关问题