获取具有子数组json值的数组的值
fetch values of the array that has a sub array json values
我有JSON响应/
{
"phweb":{
"name":"PH Web Server",
"cpu":"36.7",
"ram":"33.0",
"hdd":"43.0",
"os":{
"osup":"2 days, 23 hours, 5 minutes and 34 seconds",
"osstat":"0"
},
"apache":{
"apachestat":"0",
"apacheup":"33 days, 2 hours, 9 minutes and 16 seconds"
},
"sql":{
"sqlstat":"0",
"sqlup":"154 days, 2 hours, 16 minutes and 53 seconds"
}
},
"phdbm":{
"name":"PH DBM Server",
"cpu":"15.9",
"ram":"10.7",
"hdd":"23.7",
"os":{
"osup":"2 days, 23 hours, 6 minutes and 21 seconds",
"osstat":"0"
},
"apache":{
"apachestat":"0",
"apacheup":"103 days, 4 hours, 43 minutes and 56 seconds"
},
"sql":{
"sqlstat":"0",
"sqlup":"12 days, 4 hours, 43 minutes and 42 seconds"
}
}
}
,我可以使用
获取每个值$.each(response, function(index, value){
console.log(index + " " + value);
});
如何获取具有子数组值的响应值的值,例如
"os":{
"osup":"2 days, 23 hours, 6 minutes and 21 seconds",
"osstat":"0"
},
的"phweb"索引(假设我想从'os'的'phweb'得到'osup' ?我如何检查它是否为空或它是否存在(以避免错误)?
首先是JSON object
而不是JSON array
。
可以通过key
直接访问。
使用变量名,例如obj
,并使用.
操作符,键为该键的值。
在下面的示例中,它是obj.phweb.os
,这意味着obj是具有多个键值对的JSON对象,包括键phweb
和phweb
和os
。
请记住: JSON对象有key value
和{}
,而数组有索引和[]
。
var obj = {
"phweb":{
"name":"PH Web Server",
"cpu":"36.7",
"ram":"33.0",
"hdd":"43.0",
"os":{
"osup":"2 days, 23 hours, 5 minutes and 34 seconds",
"osstat":"0"
},
"apache":{
"apachestat":"0",
"apacheup":"33 days, 2 hours, 9 minutes and 16 seconds"
},
"sql":{
"sqlstat":"0",
"sqlup":"154 days, 2 hours, 16 minutes and 53 seconds"
}
},
"phdbm":{
"name":"PH DBM Server",
"cpu":"15.9",
"ram":"10.7",
"hdd":"23.7",
"os":{
"osup":"2 days, 23 hours, 6 minutes and 21 seconds",
"osstat":"0"
},
"apache":{
"apachestat":"0",
"apacheup":"103 days, 4 hours, 43 minutes and 56 seconds"
},
"sql":{
"sqlstat":"0",
"sqlup":"12 days, 4 hours, 43 minutes and 42 seconds"
}
}
}
console.log(obj.phweb.os);
相关文章:
- 为DataTables aoColumnDefs创建JavaScript数组(JSON格式)
- 使用 Javascript 将行添加到数组 json
- PHP数组JSON编码和该对象在ExtJs中解码
- 数组json转换为数组json
- 将Blob文本转换为Javascript中的数组/JSON对象
- 如何合并这些数组/json 对象
- 尝试从数组 JSON 键中删除引号
- JavaScript 中的数组 JSON
- 在数组 JSON 格式的 JavaScript 变量末尾添加新地址
- 如何访问这个Javascript数组(JSON对象?)
- rails传递记录数组(JSON)
- 按值提取对象/数组json vs数组
- 获取具有子数组json值的数组的值
- Object对象数组Json.字符串数组的字符串化
- 如何在Ext js 4.1中过滤数组json
- PHP -多维数组Json
- 正在分析数组Json以选择Tag
- 试图将字符串数据转换为数值数据,将数据放入数组的数组(Json)
- 从数组json、javascript中的specf值中删除引号
- 使用JavaScript或jQuery读取数组JSON