2012-02-28 61 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