从json响应中解析动态json对象

时间:2016-02-26 07:14:49

标签: javascript jquery json ajax parsing

我有这个动态的json对象

{
   "servers":[
      {
         "comp1":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"95.61"
               },
               "ip":{
                  "0":"192.168.1.0"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"99.5"
               },
               "ip":{
                  "0":"192.168.0.1"
               }
            }
         }
      },
      {
         "comp2":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"88.39"
               },
               "ip":{
                  "0":"198.168.1.1"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"99.88"
               },
               "ip":{
                  "0":"192.168.0.1"
               }
            }
         }
      },
      {
         "comp3":{
            "server1":{
               "status":"none",
               "ar":"none",
               "ip":"none"
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"99.97"
               },
               "ip":{
                  "0":"http:\/\/122.01.125.107"
               }
            }
         }
      },
      {
         "comp4":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"95.64"
               },
               "ip":{
                  "0":"192.168.1.0"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"95.65"
               },
               "ip":{
                  "0":"192.168.1.2"
               }
            }
         }
      },
      {
         "comp5":{
            "server1":{
               "status":"boxup",
               "ar":{
                  "0":"71.92"
               },
               "ip":{
                  "0":"192.168.1.0"
               }
            },
            "server2":{
               "status":"boxup",
               "ar":{
                  "0":"98.89"
               },
               "ip":{
                  "0":"192.168.0.3"
               }
            }
         }
      }
   ]
}

我试图用$ .each解析它(参见下文)

$.ajax({
        url:"/server-monitoring/load-servers",
        type:'post',
        dataType:'json',
        success:function(e){
            if(e.success){
                $.each(e.servers,function(index,value){
                    //log the status from server1 on every comp
                    console.log(value.server1.status);
                });
            }
        }
    });

但遗憾的是,遗憾的是,它给我一个错误(参见下文)

  

未捕获的TypeError:无法读取属性' status'未定义的

任何帮助,想法,建议,推荐,线索好吗?

3 个答案:

答案 0 :(得分:2)

从您的回复结构中,以下内容将起作用

$.each(e.servers,function(index,value){
                    //log the status from server1 on every comp
                    console.log(value['comp'+(index+1)].server1.status);
                });

输出为

boxup
boxup
none
boxup
boxup

修改

在评论中澄清并假设将有任何单个密钥 以下可以工作

$.each(e.servers,function(index,value){
                    //log the status from server1 on every comp
                    var key = Object.keys(value)[0];
                    console.log(value[key].server1.status);
                }); 

答案 1 :(得分:1)

这段代码怎么样?它还将处理动态键,对象数和动态内部对象。

$.each(data.servers, function(key, value) {
   $.each(this,function(key,value){
       var parentObj = key;      
       $.each(value,function(key,value){
          console.log(parentObj + '------'+key + '-----'+value.status);       
       });
   });
});

这是Working Fiddle

这是输出。

comp1------server1-----boxup
comp1------server2-----boxup
comp2------server1-----boxup
comp2------server2-----boxup
comp3------server1-----none
comp3------server2-----boxup
comp4------server1-----boxup
comp4------server2-----boxup
comp5------server1-----boxup
comp5------server2-----boxup

答案 2 :(得分:0)

通过观察你的JSON结构,我相信每个服务器对象(在服务器数组中)都有一个属性,比如comp1,comp2 ......这个属性是未知的。

以下代码有效。

   var comp = {}; //Just a temp variable to hold dynamic comp property
                  // value

   $.each(v.servers,function(i,v){ 
      //Loop through each key in server object to find first property.
      for(var key in v) {
        //Make sure its objects own property, just to be safe.
        if(v.hasOwnProperty(key)) {
            // Fetch our comp (assumed, you can also match key 
            // and make sure it starts with comp ...
            comp = v[key];
            break;
        }
      }
      //As we have our comp object, now we can access server1, server2 as shown below
      console.log(comp.server1.status);
    });

希望这有帮助!!如果您需要进一步的帮助,请告诉我.... :)