如何从javascript中读取JSON中的嵌套数组?

时间:2014-06-03 05:02:04

标签: javascript jquery json cordova

我有一个像下面这样的json数组。

[
[
"{"category_id":1,"category_name":"cake","image_id":0}",
"{"category_id":2,"category_name":"briyani","image_id":6}",
"{"category_id":3,"category_name":"Indian Breads","image_id":0}",
"{"category_id":4,"category_name":"Tandoori","image_id":5}",
"{"category_id":5,"category_name":"Seafood Delight","image_id":10}",
"{"category_id":6,"category_name":"Vegetarian","image_id":0}",
"{"category_id":7,"category_name":"Curry","image_id":0}",
"{"category_id":8,"category_name":"Biryani","image_id":0}",
"{"category_id":9,"category_name":"Dessert","image_id":13}",
"{"category_id":10,"category_name":"Chefs Recommendation","image_id":0}",
"{"category_id":11,"category_name":"Beverages","image_id":11}",
"{"category_id":12,"category_name":"Alcohol","image_id":4}",
"{"category_id":13,"category_name":"Buffet","image_id":0}"
],
[
"{"subcategory_id":1,"category_id":12,"subcategory_name":"Beer","image_id":0}",
"{"subcategory_id":2,"category_id":12,"subcategory_name":"Wine","image_id":0}"
],
[
"{"sub_id":1,"category_id":12,"subcategory_name":"Beer","image_id":0}",
"{"sub_id":2,"category_id":12,"subcategory_name":"Wine","image_id":0}"
],

如何从java脚本中只读取category_name?我想从java脚本中读取所有类别名称。

1 个答案:

答案 0 :(得分:0)

var data = {
items: [
array1:[
{"category_id":1,"category_name":"cake","image_id":0},
{"category_id":2,"category_name":"briyani","image_id":6},
{"category_id":3,"category_name":"Indian Breads","image_id":0},
{"category_id":4,"category_name":"Tandoori","image_id":5},
{"category_id":5,"category_name":"Seafood Delight","image_id":10},
{"category_id":6,"category_name":"Vegetarian","image_id":0},
{"category_id":7,"category_name":"Curry","image_id":0},
{"category_id":8,"category_name":"Biryani","image_id":0},
{"category_id":9,"category_name":"Dessert","image_id":13},
{"category_id":10,"category_name":"Chefs Recommendation","image_id":0},
{"category_id":11,"category_name":"Beverages","image_id":11},
{"category_id":12,"category_name":"Alcohol","image_id":4},
{"category_id":13,"category_name":"Buffet","image_id":0}
],
array2:[
{"subcategory_id":1,"category_id":12,"subcategory_name":"Beer","image_id":0},
{"subcategory_id":2,"category_id":12,"subcategory_name":"Wine","image_id":0}
],
array3:[
{"sub_id":1,"category_id":12,"subcategory_name":"Beer","image_id":0},
{"sub_id":2,"category_id":12,"subcategory_name":"Wine","image_id":0}
],]
};

您可以阅读以下代码段:

for(var i = 0, l = data.items.array1.length; i < l; i++) {
    var local_category_name = data.items.array1[i].category_name;
}