我如何遍历此数据数组?

时间:2019-05-22 16:12:53

标签: javascript

我已将此JSON字符串分配给变量。

<script>var Countries = {"Countries": [
{"locname":"Argentina","locCountry":"AR","ProductCount":"8","BreweryCount":"2"},
{"locname":"Austria","locCountry":"AT","ProductCount":"5","BreweryCount":"2"},
{"locname":"Belgium","locCountry":"BE","ProductCount":"19","BreweryCount":"4"},
{"locname":"Brazil","locCountry":"BR","ProductCount":"23","BreweryCount":"3"},
{"locname":"Canada","locCountry":"CA","ProductCount":"36","BreweryCount":"7"},
{"locname":"Chile","locCountry":"CL","ProductCount":"1","BreweryCount":"1"},
{"locname":"China","locCountry":"CN","ProductCount":"97","BreweryCount":"6"},
{"locname":"Dominica","locCountry":"DM","ProductCount":"1","BreweryCount":"1"},
{"locname":"France","locCountry":"FR","ProductCount":"42","BreweryCount":"2"},
{"locname":"Germany","locCountry":"DE","ProductCount":"26","BreweryCount":"3"},
{"locname":"Guatemala","locCountry":"GT","ProductCount":"1","BreweryCount":"1"},
{"locname":"Israel","locCountry":"IL","ProductCount":"1","BreweryCount":"1"},
{"locname":"Italy","locCountry":"IT","ProductCount":"5","BreweryCount":"1"},
{"locname":"Japan","locCountry":"JP","ProductCount":"47","BreweryCount":"1"},
{"locname":"South Korea","locCountry":"KR","ProductCount":"6","BreweryCount":"2"},
{"locname":"Lithuania","locCountry":"LT","ProductCount":"3","BreweryCount":"1"},
{"locname":"Mexico","locCountry":"MX","ProductCount":"24","BreweryCount":"4"},
{"locname":"Netherlands","locCountry":"NL","ProductCount":"21","BreweryCount":"3"},
{"locname":"Romania","locCountry":"RO","ProductCount":"1","BreweryCount":"1"},
{"locname":"Russia","locCountry":"RU","ProductCount":"1","BreweryCount":"1"},
{"locname":"South Africa","locCountry":"ZA","ProductCount":"5","BreweryCount":"2"},
{"locname":"Spain","locCountry":"ES","ProductCount":"79","BreweryCount":"7"},
{"locname":"Turkey","locCountry":"TR","ProductCount":"8","BreweryCount":"1"},
{"locname":"United Arab Emirates","locCountry":"AE","ProductCount":"1","BreweryCount":"1"},
{"locname":"United States","locCountry":"US","ProductCount":"687","BreweryCount":"33"},
{"locname":"Viet Nam","locCountry":"VN","ProductCount":"5","BreweryCount":"2"},
{"locname":"United Kingdom","locCountry":"GB","ProductCount":"8","BreweryCount":"3"}]}

我该如何循环以获取不同的值? 我已经尝试过Countrys [1] .locname,但这使我无法读取属性名称错误。我究竟做错了什么?

1 个答案:

答案 0 :(得分:-2)

这应该提供见识

var Countries = {
  "Countries": [{
      "locname": "Argentina",
      "locCountry": "AR",
      "ProductCount": "8",
      "BreweryCount": "2"
    },
    {
      "locname": "Austria",
      "locCountry": "AT",
      "ProductCount": "5",
      "BreweryCount": "2"
    },
    {
      "locname": "Belgium",
      "locCountry": "BE",
      "ProductCount": "19",
      "BreweryCount": "4"
    },
    {
      "locname": "Brazil",
      "locCountry": "BR",
      "ProductCount": "23",
      "BreweryCount": "3"
    },
    {
      "locname": "Canada",
      "locCountry": "CA",
      "ProductCount": "36",
      "BreweryCount": "7"
    },
    {
      "locname": "Chile",
      "locCountry": "CL",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "China",
      "locCountry": "CN",
      "ProductCount": "97",
      "BreweryCount": "6"
    },
    {
      "locname": "Dominica",
      "locCountry": "DM",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "France",
      "locCountry": "FR",
      "ProductCount": "42",
      "BreweryCount": "2"
    },
    {
      "locname": "Germany",
      "locCountry": "DE",
      "ProductCount": "26",
      "BreweryCount": "3"
    },
    {
      "locname": "Guatemala",
      "locCountry": "GT",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "Israel",
      "locCountry": "IL",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "Italy",
      "locCountry": "IT",
      "ProductCount": "5",
      "BreweryCount": "1"
    },
    {
      "locname": "Japan",
      "locCountry": "JP",
      "ProductCount": "47",
      "BreweryCount": "1"
    },
    {
      "locname": "South Korea",
      "locCountry": "KR",
      "ProductCount": "6",
      "BreweryCount": "2"
    },
    {
      "locname": "Lithuania",
      "locCountry": "LT",
      "ProductCount": "3",
      "BreweryCount": "1"
    },
    {
      "locname": "Mexico",
      "locCountry": "MX",
      "ProductCount": "24",
      "BreweryCount": "4"
    },
    {
      "locname": "Netherlands",
      "locCountry": "NL",
      "ProductCount": "21",
      "BreweryCount": "3"
    },
    {
      "locname": "Romania",
      "locCountry": "RO",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "Russia",
      "locCountry": "RU",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "South Africa",
      "locCountry": "ZA",
      "ProductCount": "5",
      "BreweryCount": "2"
    },
    {
      "locname": "Spain",
      "locCountry": "ES",
      "ProductCount": "79",
      "BreweryCount": "7"
    },
    {
      "locname": "Turkey",
      "locCountry": "TR",
      "ProductCount": "8",
      "BreweryCount": "1"
    },
    {
      "locname": "United Arab Emirates",
      "locCountry": "AE",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "United States",
      "locCountry": "US",
      "ProductCount": "687",
      "BreweryCount": "33"
    },
    {
      "locname": "Viet Nam",
      "locCountry": "VN",
      "ProductCount": "5",
      "BreweryCount": "2"
    },
    {
      "locname": "United Kingdom",
      "locCountry": "GB",
      "ProductCount": "8",
      "BreweryCount": "3"
    }
  ]
};
console.log(Countries.Countries);
console.log(Countries.Countries[1]);
console.log(Countries.Countries[1].locname);
for (let i = 0; i < Countries.Countries.length; i++) {
  console.log(Countries.Countries[i]);
}