将JavaScript中的JSON数组解析为多个变量

时间:2013-10-22 07:13:38

标签: javascript arrays json

我在下面有一个JSON字符串,它在JavaScript中存储为var。我试图将字符串的各个部分解析为变量。

特别是,我需要地址,邮政编码,地区和地区。

此JSON数组存储为名为“data”的JS var

有谁知道如何开始解析这些东西?谢谢大家!

[{"address":"2801 Elliott Ave","category_ids":[347],"category_labels":[["Social","Food and 
Dining","Restaurants"]],"country":"us","email":"kimd@thedussingroup.com","factual_id":"43cfe23
8-ae8e-469a-8592-a1edc8603051","fax":"(206) 448-
9252","latitude":47.615154,"locality":"Seattle","longitude":-122.353724,"name":"The Old 
Spaghetti Factory","neighborhood":["Belltown","Downtown","Downtown 
Seattle"],"postcode":"98121","region":"WA","tel":"(206) 441-
7724","website":"http:\/\/www.osf.com"}]

感谢帮助!

3 个答案:

答案 0 :(得分:3)

你JSON是一个数组(因为它包含在[]中),所以你需要:

var data = JSON.parse('[{"addre....}]');
var address = data[0].address,
    postcode = data[0].postcode;

依旧......

答案 1 :(得分:1)

for(var i in data[0]){
    window[i] = data[0][i];
}

alert(address);

答案 2 :(得分:0)

您可以使用 JSON.parse

执行此操作
var data= your json;
JSON.parse(data);

<强>更新 在你的情况下,你甚至不需要解析你可以直接使用

console.log(data[0].address); //returns 2801 Elliott Ave 
console.log(data[0].category_ids); //returns [347] 

选中此JSFiddle