在 ajax 请求后访问 JSON 中的元素
Access elements in JSON after ajax request
我花了几个小时试图弄清楚这一点。我使用 JQuery 发出 ajax 请求。我收到字符串格式的响应,我使用
jQuery.parseJSON(response);
以将其转换为对象。这是我的回应:
{
"columns": ["n"],
"data": [
[{
"extensions": {},
"labels": "http://localhost:7474/db/data/node/168/labels",
"outgoing_relationships": "http://localhost:7474/db/data/node/168/relationships/out",
"traverse": "http://localhost:7474/db/data/node/168/traverse/{returnType}",
"all_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/all/{-list|&|types}",
"property": "http://localhost:7474/db/data/node/168/properties/{key}",
"self": "http://localhost:7474/db/data/node/168",
"properties": "http://localhost:7474/db/data/node/168/properties",
"outgoing_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/out/{-list|&|types}",
"incoming_relationships": "http://localhost:7474/db/data/node/168/relationships/in",
"create_relationship": "http://localhost:7474/db/data/node/168/relationships",
"paged_traverse": "http://localhost:7474/db/data/node/168/paged/traverse/{returnType}{?pageSize,leaseTime}",
"all_relationships": "http://localhost:7474/db/data/node/168/relationships/all",
"incoming_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/in/{-list|&|types}",
"metadata": {
"id": 168,
"labels": []
},
"data": {
"name": "1"
}
}]
]
}
我尝试访问此对象的特定元素,但我没有得到任何回报。当我尝试这个
var test = json.data;
它有效,但我如何访问保存在元数据中的值。我尝试这个,但我得到"未定义":
var test = json.data.metadata.id;
知道我错过了什么吗?
它是一个数组,所以你必须使用索引
var test = json.data[0][0].metadata.id;
json.data[0]
返回[{...}]
,[{...}][0]
再次返回具有metadata
对象的{...}
,而对象又作为您需要id
属性。
json.data 是一个双精度数组。
这应该有效:
var test = json.data[0][0].metadata.id;
尝试:
var test = json.data[0][0].metadata.id;
json.data
是一个数组数组。
相关文章:
- 访问JSON对象内部的数组元素
- Jquery移除或更改JSON根元素
- 元素名称上带有短划线 (-) 字符的 Json 对象
- 如何在html angular中显示嵌套的json元素
- 如何访问JSON元素
- 使用Underscore.js修改json数组中所选元素的更有效方法
- 从json数组中查找满足条件的特定元素的值
- 在JSON文件中查找一个元素,并将其显示为HTML
- 如何在php中按元素按字母顺序排列json文件
- 在JSON数组中进行迭代,并为其元素设置样式
- 从JSON.parse获取元素
- 使用JavaScript在Json中提取时,将数组的元素转换为String
- 如何使用javascript获取json元素
- 隐藏HTML元素中的JSON编码和解码
- 使用JSON和JavaScript添加额外的html列表元素
- 通过 id json, jquery 选择元素
- 如何使克隆的 DOM 元素 JSON 可序列化
- 循环元素json数组
- 获取数组中的元素(json格式)
- 添加元素 JSON