简体   繁体   English

从JSON响应解析动态JSON对象

[英]Parse a dynamic json objects from a json response

I have this dynamic json object 我有这个动态的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"
               }
            }
         }
      }
   ]
}

and I tried to parse it with $.each (refer below) 我试图用$ .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);
                });
            }
        }
    });

but unfortunately and sadly, it returns me an error (refer below) 但不幸的是,它返回一个错误(请参阅下文)

Uncaught TypeError: Cannot read property 'status' of undefined 未捕获的TypeError:无法读取未定义的属性“状态”

any help, ideas, suggestions, recommendation, clues please? 有任何帮助,想法,建议,建议,线索吗?

From your structure of response, following will work 根据您的回应结构,以下将起作用

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

Gives output as 给出为

boxup
boxup
none
boxup
boxup

Edit: 编辑:

After clarification in comment and assuming there will be any single key Below can work 在评论中澄清后,假设将有任何单个键,下面的方法可以正常工作

$.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);
                }); 

How about this code. 这段代码怎么样。 It will handle dynamic keys, number of objects and dynamic inner objects too. 它将处理动态键,对象数和动态内部对象。

$.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);       
       });
   });
});

Here is a Working Fiddle 这是一个工作小提琴

And here is the output. 这是输出。

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

By Observing your JSON structure, I believe that each server object (in servers array) has one property like comp1, comp2 ... which is not known. 通过观察您的JSON结构,我相信每个服务器对象(在服务器阵列中)都具有一个属性,例如comp1,comp2 ...,这是未知的。

Following code works. 以下代码有效。

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

Hope this helps !! 希望这可以帮助 !! Let me know if you need further assistance .... :) 让我知道您是否需要进一步的帮助.... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM