简体   繁体   English

如何从此数组中获取某些对象的值?

[英]How do I get the value of certain objects from this array?

I want to get the value of "city" and "country".我想获得“城市”和“国家”的价值。

Here's my HTML and javascript code.这是我的 HTML 和 javascript 代码。

I've tried:我试过了:

  • results.city结果.city
  • results["city"] but they don't seem to work; results["city"] 但它们似乎不起作用; giving me undefined value.给我未定义的价值。
  <pre id="json-result"></pre>
  <p><span id="city"></span></p>
  <p><span id="country"></span></p>

  <script>
    var result = document.getElementById("json-result");
    const Http = new XMLHttpRequest();

    window.addEventListener("load", function () {
      console.log("getLocation Called");
      var bdcApi = "https://api.bigdatacloud.net/data/reverse-geocode-client"

      navigator.geolocation.getCurrentPosition(
        (position) => {
          bdcApi = bdcApi +
            "?latitude=" + position.coords.latitude +
            "&longitude=" + position.coords.longitude +
            "&localityLanguage=en";
          getApi(bdcApi);

        },
        (err) => {
          getApi(bdcApi);
        }, {
          enableHighAccuracy: true,
          timeout: 5000,
          maximumAge: 0
        });
    })

    function getApi(bdcApi) {
      Http.open("GET", bdcApi);
      Http.send();
      Http.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
          result.innerHTML = this.responseText;
          var results = this.responseText
          document.getElementById("city").innerHTML = results.city
        }
      };
    }
  </script>

You need to convert the API's text response into an actual Javascript object before you attempt to access its members.在尝试访问其成员之前,您需要将 API 的文本响应转换为实际的 Javascript object。 You can convert JSON text into a Javascript object by using JSON.parse() .您可以使用JSON.parse()将 JSON 文本转换为 Javascript object。

Try this instead:试试这个:

const results = JSON.parse(this.responseText);

(As a side note, if you don't need to support Internet Explorer, you will likely be better off using the newer and easier-to-use fetch instead of XMLHttpRequest .) (附带说明,如果您不需要支持 Internet Explorer,则最好使用更新且更易于使用的fetch而不是XMLHttpRequest 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何过滤包含特定值的唯一对象的数组? - How do I filter an array for unique objects that contain a certain value? 如何使用 json 从多个对象中获取某个属性值? - How do I get certain a property value from multiple objects using json? 如何从对象数组及其在JavaScript中的索引中获取最小值? - How do I get the lowest value from an array of objects and its index in JavaScript? 如何从 JavaScript 对象数组中获取键值对? - How do I get key-value pair from array of JavaScript objects? 如何确定数组中一定数量的对象是否在JS中包含具有相同值的相同属性 - How do I determine if a certain amount of objects in an array contain the same property with the same value in JS 如何从该对象数组获得以下输出? - How do I get the following output from this array of objects? 如何从 object 获取数据并创建对象数组? - How do i get data from object and create array of objects? 如何从 JavaScript 中的对象数组中获取唯一值,但按特定值过滤 - How to get unique values from an array of objects in JavaScript, but filtered by certain value 除非它包含具有特定值的另一个项目,否则如何从对象数组中获取具有属性的列表? - How to get list with of property from array of objects unless it contains another item with certain value? 如何从这些对象中的任何一个获取此值 - How do I get this value from either of these objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM