如何从以下json访问“名称和商店名称”

时间:2012-10-13 08:32:19

标签: javascript json

  

可能重复:
  I have a nested data structure / JSON, how can access a specific value?

{
"List":
  [
     {"Active":true,"Name":"VMW","Stores":
      [
        {"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}
      ]
     }
  ]
}

它的json数据,如何使用Ajax或Javasricpt

读取它

2 个答案:

答案 0 :(得分:1)

List和商店是一个数组,所以要检索名称和商店的名称,请使用这样的数组索引:

jsondata.List[0].Name >>> return "VMW"
jsondata.List[0].Stores[0].Name >>> return "Admin"

答案 1 :(得分:0)

json = {
"List":
  [
     {"Active":true,"Name":"VMW","Stores":
      [
        {"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}
      ]
     }
  ]
}

json["List"]  // { "Active":true,"Name":"VMW","Stores": [{"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}]}


json["List"][0]["Stores"] // {"Active":true,"Name":"Admin"},{"Active":true,"Name":"sunil"}

是对象的商店

Stores = json["List"][0]["Stores"]

for (i in Stores) (function(active, name) {


    console.log(active, name);
}(Stores[i]["Active"], Stores[i]["Name"]));

结果:

true "Admin"
true "sunil"