gpt4 book ai didi

javascript - 循环 JSON 响应给出未定义

转载 作者:行者123 更新时间:2023-12-02 15:34:18 25 4
gpt4 key购买 nike

这是 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);
});
}

但它给了我“未定义”。有什么想法、帮助、线索吗?

最佳答案

您正在迭代 json 中的第一个对象,该对象不包含任何对象,只有字符串。将 response.phweb 更改为 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>

更新:如果您想从 phweb 获取 cpu 值,则不需要 $.each() ~ 只需使用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);

关于javascript - 循环 JSON 响应给出未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33074824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com