JavaScript JSON 数组

标签 javascript json api

我正在使用来自服务器 API 的 get 请求,我正在发出请求,然后执行以下操作:

var resp = JSON.parse(response);

我调用提供 0001 和 0002 作为参数的服务器,并且 在 JSON.parse 之后它返回一个数组,例如:

{"0001":{"id":1},"0002":{"id":2}}

我知道传统上如果我得到静态响应,例如

{"placeID":{"id":1},"placeID":{"id":2}}

我可以这样做:

resp.placeId.id

但鉴于返回名称并不总是相同,我如何访问第一个值 resp.0001.id鉴于0001可能并不总是返回的值?

最佳答案

使用for...in循环。

for(var property in resp) {
    console.log(property + ": " + resp[property]);
}

关于JavaScript JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25977333/

相关文章:

javascript - Openlayers,更改聚类标记

javascript - jQuery - 检查函数是否已定义

javascript - Web Worker 在 HTML5 Canvas 处理中内存不足

python - 在 mysql JSON 字段的 cursor.execute() 中使用 '.format()' 与 '%s',使用 Python mysql.connector,

javascript - 如何检测 json 不是 json (它是 html)

javascript - 傻瓜的 Getter\setter

jQuery Ajax setTimeout JSON

javascript - 使用xpath从json对象快速检索数据

ruby-on-rails - 将外部 API 包装到 Ruby 类中

API key 约定