【问题标题】:Can't parse DialogFlow json response无法解析 DialogFlow json 响应
【发布时间】:2017-12-08 02:33:52
【问题描述】:

我知道这个问题已经被问过很多次了......但我不知道会发生什么 我在尝试解析 json 响应时遇到问题。 这是json(摘录)

{"result":{"fulfillment":{"speech":"[{\"name\":\"Pallet truck\",\"object_type\":\"machine\",\"object_id\":3279}, ...

我正在像这样对其应用 JSON 解析

 var obj = JSON.parse(response);

那我有这样的东西

"result":{
  "fulfillment":{
     "speech":"[
{"name":"Pallet truck","object_type":"machine","object_id":3279},        
{"name":"CollaborativeRobot","object_type":"machine","object_id":3273},
{"name":"Bender","object_type":"machine","object_id":3997},...

我只想在最后显示它:

Name : Pallet Truck
Name : CollaborativeRobot
Name : Bender

我已经尝试过这样的事情

for (var key in obj.result.fulfillment.speech) {
      if (obj.result.fulfillment.speech.hasOwnProperty(key)) {
            console.log(obj.result.fulfillment.speech[key].name.val());
      }
}

但我只得到未定义的结果...我想我在访问数组时遗漏了一些东西(已经有一段时间没有在 php/js 中编码任何东西了)

编辑

编辑 2

似乎问题出在服务器端,而双重编码 这是摘录:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://api.mephisto.optimdata.io/search?object_type=machine');
    curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'authorization: JWT '.$accessToken));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    $fulfillment = new stdClass();
    $fulfillment->speech = $result;
    $result = new stdClass();
    $result->fulfillment = $fulfillment;
    $result->requete = "machines actives";
    $responseQueryAPI = new stdClass();
    $responseQueryAPI->result = $result;
    echo json_encode($responseQueryAPI);

也许这是因为 curl 响应已经是 json 了? curl 响应如下所示:

[
    {
        "name": "Pallet truck",
        "object_type": "machine",
        "object_id": 3279
    },
    {
        "name": "Collaborative Robot",
        "object_type": "machine",
        "object_id": 3273
    },
    {
        "name": "Bender",
        "object_type": "machine",
        "object_id": 3997
    },

【问题讨论】:

  • 看起来像 JSONception(JSON 中的 JSON .. ),因为转义引号 \"。您可以在 speech 的值上尝试 JSON.parse。类似var speech = JSON.parse(JSON.parse(responce).result.fulfillment.speech)
  • 是的,我认为可能是服务器端的错误,我将使用我的 php 代码进行编辑以查看发生了什么

标签: javascript json parsing


【解决方案1】:

name只是一个字符串,所以尝试替换

for (var key in obj.result.fulfillment.speech) {
    if (obj.result.fulfillment.speech.hasOwnProperty(key)) {
        console.log(obj.result.fulfillment.speech[key].name.val());
    }
}

for (var i = 0; i < obj.result.fulfillment.speech.length; ++i) {
    console.log(obj.result.fulfillment.speech[i].name);
}

编辑:

响应数据看起来不对。

JSON.parse('{"result":{"fulfillment":{"speech":"[{\"name\":\"Pallet truck\",\"object_type\":\"machine\",\"object_id\":3279}]"}}}'); 出现语法错误。

尝试使用 JSON.parse('{"result":{"fulfillment":{"speech":[{"name":"Pallet truck","object_type":"machine","object_id":3279}]}}}');.

【讨论】:

  • 仍然未定义结果
  • console.log(obj.result.fulfillment.speech) 怎么样?
  • 它在控制台中显示整个 json: "result":{ "fulfillment":{ "speech":"[ {"name":"Pallet truck","object_type":"machine", "object_id":3279}, ...
  • 我猜console.log(obj.result.fulfillment.speech) 是类似[[ {"name":"Pallet truck","object_type":"machine","object_id":3279},..] 的数组。所以,请尝试将obj.result.fulfillment.speech 替换为obj.result.fulfillment.speech.result.fulfillment.speech
  • 我从几个小时开始就有这个问题......我很累,但我为 (var i = 0; i
猜你喜欢
  • 2018-06-19
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多