从 Http 响应中提取文本
Extracting text from Http response
以下是java-
中http响应的截图
以下是回复的文字形式:
{
"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
}
]
}
我想提取建议中的"text"。
这是我与 json 的部分。我在 "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());
}
如果您正在寻找特定 JSON 节点的值,您可以使用 JsonPath expression 例如提取所有 Text
个节点的值:
$.LightGingerTheTextResult[*].Suggestions[*].Text
在你的例子中简化为
$..Text
或者只是第一个 Suggestions
节点的第一个 Text
节点:
$.LightGingerTheTextResult[0].Suggestions[0].Text
我建议您首先从检索 httpResponse 对象的主体开始。
String tmp = response.body(); // I assume the callback method has a an
//argument of type
//httpResponse called response
然后将其存储在某处eg:string。
使用 gson 并使用 httpResponse class
像这样:
httpResponse rep = gson.fromJson(, httpResponse .class);
通过这种方式,您现在可以使用 rep 对象来检索您想要的内容。
参见whosebug.com/questions/2591098。你需要一个图书馆,
将包 org.json 与
一起使用
new JSONObject(textOfResponse)
.getJSONArray("LightGingerTheTextResult").getJSONObject(0)
.getJSONArray("Suggestions").getJSONObject(0)
.getString("Text")
你的textOfResponse
我得到
An
以下是java-
中http响应的截图以下是回复的文字形式:
{
"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
}
]
}
我想提取建议中的"text"。
这是我与 json 的部分。我在 "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());
}
如果您正在寻找特定 JSON 节点的值,您可以使用 JsonPath expression 例如提取所有 Text
个节点的值:
$.LightGingerTheTextResult[*].Suggestions[*].Text
在你的例子中简化为
$..Text
或者只是第一个 Suggestions
节点的第一个 Text
节点:
$.LightGingerTheTextResult[0].Suggestions[0].Text
我建议您首先从检索 httpResponse 对象的主体开始。
String tmp = response.body(); // I assume the callback method has a an
//argument of type
//httpResponse called response
然后将其存储在某处eg:string。
使用 gson 并使用 httpResponse class
像这样: httpResponse rep = gson.fromJson(, httpResponse .class); 通过这种方式,您现在可以使用 rep 对象来检索您想要的内容。
参见whosebug.com/questions/2591098。你需要一个图书馆, 将包 org.json 与
一起使用new JSONObject(textOfResponse)
.getJSONArray("LightGingerTheTextResult").getJSONObject(0)
.getJSONArray("Suggestions").getJSONObject(0)
.getString("Text")
你的textOfResponse
我得到
An