如何使用jQuery或JavaScript从JSON数据中读取country_code?

时间:2017-11-15 05:19:22

标签: javascript jquery json

{
  "status": "success",
  "description": "Data successfully received.",
  "data": {
    "geo": {
      "host": "2405:204:3213:5500:b012:b9d5:e4db:47b6",
      "ip": "2405:204:3213:5500:b012:b9d5:e4db:47b6",
      "rdns": "2405:204:3213:5500:b012:b9d5:e4db:47b6",
      "asn": "AS55836",
      "isp": "Reliance Jio Infocomm Limited ",
      "country_name": "India",
      "country_code": "IN",
      "region": "",
      "city": "",
      "postal_code": "",
      "continent_code": "AS",
      "latitude": "20",
      "longitude": "77",
      "dma_code": "",
      "area_code": "",
      "timezone": "Asia/Kolkata",
      "datetime": "2017-11-15 10:12:15"
    }
  }
}

如何使用jQuery或JavaScript阅读country_code

2 个答案:

答案 0 :(得分:1)

JSON.parse是您正在寻找的。

let someJSObject = JSON.parse(someJSONString);

之后,您可以使用成员访问操作符来访问所需的字段。

答案 1 :(得分:0)

你去吧



var a = {
  "status": "success",
  "description": "Data successfully received.",
  "data": {
    "geo": {
      "host": "2405:204:3213:5500:b012:b9d5:e4db:47b6",
      "ip": "2405:204:3213:5500:b012:b9d5:e4db:47b6",
      "rdns": "2405:204:3213:5500:b012:b9d5:e4db:47b6",
      "asn": "AS55836",
      "isp": "Reliance Jio Infocomm Limited ",
      "country_name": "India",
      "country_code": "IN",
      "region": "",
      "city": "",
      "postal_code": "",
      "continent_code": "AS",
      "latitude": "20",
      "longitude": "77",
      "dma_code": "",
      "area_code": "",
      "timezone": "Asia/Kolkata",
      "datetime": "2017-11-15 10:12:15"
    }
  }
}

console.log("Country Code: "+ a.data.geo.country_code);