循环通过JSON响应给出undefined

时间:2015-10-12 06:47:56

标签: javascript jquery json

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

但是它给了我“未定义的”#。任何想法,帮助,线索?

2 个答案:

答案 0 :(得分:1)

您正在迭代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>

更新:如果您想从cpu获取phweb值,则无需$.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);

答案 1 :(得分:1)

例如: -

$.each(response.phweb, function(index,value){
          console.log(index + " : " + value);
         //alert(index.cpu);
        });