【发布时间】:2017-03-06 15:28:55
【问题描述】:
我正在使用 watson 服务并尝试为我的应用程序构建一个聊天框,来自 watson API 的响应采用 JSON 格式,如下所示:
{
"context": {
"conversation_id": "00d5c35d-ab4e-47d3-a7fd-2b78d57df59f",
"system": {
"dialog_stack": [
{
"dialog_node": "root"
}
],
"dialog_turn_counter": 1.0,
"dialog_request_counter": 1.0,
"_node_output_map": {
"node_4_1487236585212": [
0.0
]
},
"dialog_in_progress": false
}
},
"entities": [],
"intents": [
{
"confidence": 1.0,
"intent": "Hello"
},
{
"confidence": 0.0,
"intent": "GoodBye"
}
],
"output": {
"log_messages": [],
"text": [
"Welcome to my conversation app"
],
"nodes_visited": [
"node1"
]
},
"input": {
"text": "hii"
}
}
现在在我的应用程序中,我需要从 JSON 响应中获取“输出”键的数据,并从“输出”键中获取“文本”键。
我想要做的如下:
MessageRequest options = new MessageRequest.Builder().inputText(message).alternateIntents(true).context(context).build();
MessageResponse response = service.message("93576d15-15a9-4029-bc07-41da7f2efa39", options).execute();
context = response.getContext();
/*System.out.println(context);*/
System.out.println(response);
try {
JSONObject contObj = new JSONObject(response);
System.out.println(contObj.getString("output"));
JSONObject contObj1 = new JSONObject(contObj);
System.out.println(contObj1);
System.out.println("TEXT OUTPUT"+contObj1.getString("text"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
现在包含“输出”的 sysout 给出了正确的值,但包含“文本”的 sysout 给出了 null。 任何帮助将不胜感激。谢谢
【问题讨论】:
-
您要访问哪个文本值?
-
嗨,如果您在 JSON 共享中看到,有一个关键的“输出”,其中有一个关键的“文本”,我说的是那个
标签: arrays json watson-conversation watson