从 JSON 对象获取数据时出现问题

Issue with getting data from a JSON object

本文关键字:问题 数据 JSON 对象 获取      更新时间:2023-09-26

我有一个来自Ajax POST请求的响应,如下所示:

{
    "columns": [
        "r"
    ],
    "data": [
        [
            {
                "extensions": {},
                "start": "http://localhost:7474/db/data/node/2762",
                "property": "http://localhost:7474/db/data/relationship/2709/properties/{key}",
                "self": "http://localhost:7474/db/data/relationship/2709",
                "properties": "http://localhost:7474/db/data/relationship/2709/properties",
                "type": "IS_CONNECTED_WITH",
                "end": "http://localhost:7474/db/data/node/2763",
                "metadata": {
                    "id": 2709,
                    "type": "IS_CONNECTED_WITH"
                },
                "data": {
                    "FOC_Type": "IRU",
                    "DuctType": "IRU",
                    "TrenchType": "IRU",
                    "linestringId": "53805"
                }
            }
        ]
    ]
}

上面是一个字符串。我试图访问的是元素:"FOC_Type":"IRU""DuctType":"IRU""TrenchType":"IRU""linestringId":"53805"

我将字符串转换为 JSON,如下所示:

  var response = JSON.parse(response);

然后我尝试访问一些这样的值:

  var dataEquip = response[Object.keys(response)[Object.keys(response).length - 1]] // get the data from json obj
  var komvosName = dataEquip[0][2];

但我不能让它工作。

我找到了一个解决方法,其中我不将响应转换为 JSON 格式,而是使用字符串。但这并不好。如果有人能告诉我我做错了什么,我将不胜感激。

做 :

var responseJSON = JSON.parse(response);
var dataEquip = responseJSON ['data'] // get the data from json obj
var komvosName = dataEquip['TrenchType'];

JSON对象(responseJSON)只不过是一个关联数组。