2012-02-28 66 views
1
{ 
"info": { 
    "limit": "0", 
    "xdata": { 
     "hospital": { 
      "name": "qwewe", 
      "street": "cxcxc" 
     }, 
     "factory": { 
      "name": "wrwr", 
      "street": "xzcc" 
     }, 
     "industry": { 
      "name": "lll", 
      "street": "sdfdsfdf" 
     } 
    } 
    } 

如何打印xdata的内容?即我想要医院,工厂的数据&工业 我正在使用$.each jQuery循环,但无法得到医院的细节。在json中循环 - jquery

+0

这是一个有效的JSON(在jsonlint CHKD)..只是复制它的一部分.. – user1184100 2012-02-28 10:17:37

+0

your_data.info .xdata.hospital.name; – Vytautas 2012-02-28 10:20:59

+0

最后你错过了一个}。不幸的是我不能自己编辑这个问题。 – Basil 2012-02-28 10:27:00

回答

3

尝试下一

xdata = your_data.info.xdata; 

for (xd in xdata) { 
    console.log('Name: ' + xdata[xd].name); 
    console.log('Street: ' + xdata[xd].street); 
} 
6
var data = { 
"info": { 
    "limit": "0", 
    "xdata": { 
     "hospital": { 
      "name": "qwewe", 
      "street": "cxcxc" 
     }, 
     "factory": { 
      "name": "wrwr", 
      "street": "xzcc" 
     }, 
     "industry": { 
      "name": "lll", 
      "street": "sdfdsfdf" 
     } 
    } 
}}; 

$.each(data.info.xdata, function(key, value) { 
    var type = key; // e.g. hospital/factory/industry 
    var name = value.name; 
    var street = value.street; 

    // do something with the values 
    console.log(type, name, street); 
}); 

喜欢这个?说实话,JSON并没有真正格式化。 XDATA最好包含一组对象,但是meh。

欲了解更多信息,请参阅此jsfiddle:http://jsfiddle.net/fZBYG/1/请务必打开您的控制台。

1
$.each(info.xdata, function(key, value) { 
    // key is equal to hospital, factory, industry 
    // valus is equal to { "name": "qwewe", "street": "cxcxc" }, ... 
    // this in the scope is the same as arguments[1] - value; 
    // this === value 
}); 
1
var json = $.parseJSON(j); 
//console.log(json.info.xdata); 
$.each(json.info.xdata,function(k,v){ 
console.log(v.name+" -- "+ v.street); 
}); 

DEMO