通过JSON响应的循环给出未定义
loop through JSON response gives undefined
这是JSON响应
{
"phweb":{
"cpu":"34.8",
"ram":"30.5",
"hdd":"42.9",
"osup":"2 days, 20 hours, 45 minutes and 28 seconds",
"osstat":"0",
"apachestat":"0",
"apacheup":"32 days, 23 hours, 49 minutes and 13 seconds",
"sqlstat":"0",
"sqlup":"13305410"
},
"phdbm":{
"cpu":"14.7",
"ram":"10.8",
"hdd":"23.7",
"osup":"2 days, 20 hours, 46 minutes and 15 seconds",
"osstat":"0",
"apachestat":"0",
"apacheup":"103 days, 2 hours, 23 minutes and 53 seconds",
"sqlstat":"0",
"sqlup":"12 days, 2 hours, 23 minutes and 39 seconds"
},
"phmail":{
"cpu":"0.8",
"ram":"27.4",
"hdd":"7.3",
"osup":"2 days, 20 hours, 39 minutes and 29 seconds",
"osstat":"0",
"apachestat":"0",
"apacheup":"5 days, 3 hours, 54 minutes and 29 seconds",
"sqlstat":"0",
"sqlup":"5 days, 3 hours, 54 minutes and 27 seconds",
"emailstat":"0",
"emailup":"2 days, 20 hours, 39 minutes and 29 seconds"
},
"ghweb":{
"cpu":"0.4",
"ram":"10.9",
"hdd":"22.4",
"osup":"2 days, 21 hours, 42 minutes and 17 seconds",
"osstat":"0",
"apachestat":"0",
"apacheup":"2 days, 23 hours, 44 minutes and 23 seconds",
"sqlstat":"0",
"sqlup":"2 days, 23 hours, 44 minutes and 22 seconds"
},
"ghdbm":{
"cpu":"0.5",
"ram":"26.3",
"hdd":"6.0",
"osup":"2 days, 20 hours, 51 minutes and 58 seconds",
"osstat":"0",
"apachestat":"0",
"apacheup":"103 days, 2 hours, 53 minutes and 50 seconds",
"sqlstat":"0",
"sqlup":"109 days, 21 hours, 3 minutes and 0 seconds"
}
}
,我试着循环它并得到我想要的值
success: function(response){
$.each(response.phweb, function(index, value){
alert(value.cpu);
});
}
但是它给了我'undefine'。有什么想法、帮助或线索吗?
你是从json中迭代第一个对象,它不包含任何对象,只有字符串在那里。Change response.phweb
to response
var response = {
"phweb": {
"cpu": "34.8",
"ram": "30.5",
"hdd": "42.9",
"osup": "2 days, 20 hours, 45 minutes and 28 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "32 days, 23 hours, 49 minutes and 13 seconds",
"sqlstat": "0",
"sqlup": "13305410"
},
"phdbm": {
"cpu": "14.7",
"ram": "10.8",
"hdd": "23.7",
"osup": "2 days, 20 hours, 46 minutes and 15 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "103 days, 2 hours, 23 minutes and 53 seconds",
"sqlstat": "0",
"sqlup": "12 days, 2 hours, 23 minutes and 39 seconds"
},
"phmail": {
"cpu": "0.8",
"ram": "27.4",
"hdd": "7.3",
"osup": "2 days, 20 hours, 39 minutes and 29 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "5 days, 3 hours, 54 minutes and 29 seconds",
"sqlstat": "0",
"sqlup": "5 days, 3 hours, 54 minutes and 27 seconds",
"emailstat": "0",
"emailup": "2 days, 20 hours, 39 minutes and 29 seconds"
},
"ghweb": {
"cpu": "0.4",
"ram": "10.9",
"hdd": "22.4",
"osup": "2 days, 21 hours, 42 minutes and 17 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "2 days, 23 hours, 44 minutes and 23 seconds",
"sqlstat": "0",
"sqlup": "2 days, 23 hours, 44 minutes and 22 seconds"
},
"ghdbm": {
"cpu": "0.5",
"ram": "26.3",
"hdd": "6.0",
"osup": "2 days, 20 hours, 51 minutes and 58 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "103 days, 2 hours, 53 minutes and 50 seconds",
"sqlstat": "0",
"sqlup": "109 days, 21 hours, 3 minutes and 0 seconds"
}
};
$.each(response, function(index, value) {
alert(value.cpu);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
UPDATE:如果你想从phweb
得到cpu
的值,那么就不需要$.each()~ just use
response.phweb.cpu '
var response = {
"phweb": {
"cpu": "34.8",
"ram": "30.5",
"hdd": "42.9",
"osup": "2 days, 20 hours, 45 minutes and 28 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "32 days, 23 hours, 49 minutes and 13 seconds",
"sqlstat": "0",
"sqlup": "13305410"
},
"phdbm": {
"cpu": "14.7",
"ram": "10.8",
"hdd": "23.7",
"osup": "2 days, 20 hours, 46 minutes and 15 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "103 days, 2 hours, 23 minutes and 53 seconds",
"sqlstat": "0",
"sqlup": "12 days, 2 hours, 23 minutes and 39 seconds"
},
"phmail": {
"cpu": "0.8",
"ram": "27.4",
"hdd": "7.3",
"osup": "2 days, 20 hours, 39 minutes and 29 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "5 days, 3 hours, 54 minutes and 29 seconds",
"sqlstat": "0",
"sqlup": "5 days, 3 hours, 54 minutes and 27 seconds",
"emailstat": "0",
"emailup": "2 days, 20 hours, 39 minutes and 29 seconds"
},
"ghweb": {
"cpu": "0.4",
"ram": "10.9",
"hdd": "22.4",
"osup": "2 days, 21 hours, 42 minutes and 17 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "2 days, 23 hours, 44 minutes and 23 seconds",
"sqlstat": "0",
"sqlup": "2 days, 23 hours, 44 minutes and 22 seconds"
},
"ghdbm": {
"cpu": "0.5",
"ram": "26.3",
"hdd": "6.0",
"osup": "2 days, 20 hours, 51 minutes and 58 seconds",
"osstat": "0",
"apachestat": "0",
"apacheup": "103 days, 2 hours, 53 minutes and 50 seconds",
"sqlstat": "0",
"sqlup": "109 days, 21 hours, 3 minutes and 0 seconds"
}
};
alert(response.phweb.cpu);
例如:-
$.each(response.phweb, function(index,value){
console.log(index + " : " + value);
//alert(index.cpu);
});
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- $.每个jquery循环打印一个“;未定义的“;对于getJSON请求后的每个元素,网格数据都会完美地打印出来
- 使用arr[i]循环遍历数组==未定义
- 为什么在循环中返回时函数返回值未定义
- 未捕获的类型错误:未定义不是函数,在中为循环创建了对象
- Waypoint的循环,退出循环后属性未定义
- 对象在循环中运行时未定义,但在按顺序执行时未定义
- 使用while循环在printArray函数中获取一个额外的未定义值
- 如何使此循环工作?它's返回未定义的语言js
- Javascript数组中未定义的值是否使用任何内存或在for in循环中迭代
- 未捕获的typeerror无法读取属性'0'for循环中未定义的
- 使用jQuery'在数组中循环;s中的每一个都会在生成字符串时在开头产生一个未定义的值
- 嵌套的“for”循环 - 数组未定义
- 在 For 循环中的闭包中未定义
- javascript 中的框架对象循环是未定义的
- 为什么以下 forEach 数组循环未返回未定义
- 当在 Javascript 中使用许多 for 循环时,输出是未定义的
- 用于从许多带有“for”循环的URL中获取HTML信息的X射线使对象未定义
- 当我把它放到setInterval循环中时,event.clientY是未定义的
- 除非调用console.log,否则未定义循环引用父子关系