简体   繁体   English

使用 Fetch 获取 API 响应,使用 JSON.stringify(),然后...?

[英]Using Fetch to get an API response, using JSON.stringify(), and then...?

I have searched this nine ways from Sunday, but there's something I'm missing here, and I don't know what it is.从周日开始,我已经搜索了这九种方式,但是我在这里缺少一些东西,我不知道它是什么。

I'm using fetch to grab some data from Mapbox:我正在使用 fetch 从 Mapbox 中获取一些数据:

var response = fetch(myURL)

.then(response => response.json())
.then (data => console.log(JSON.stringify(data)))

When I run this and look at the console, I can see that I have indeed grabbed the data, because it gets logged.当我运行它并查看控制台时,我可以看到我确实抓取了数据,因为它被记录了。 (I'm putting what got logged at the end because it's a lot.) (我将记录的内容放在最后,因为它很多。)

The problem is I don't know how to get the information into a format where I can work with it.问题是我不知道如何将信息转换为我可以使用的格式。 All I want is the lat/long, but I can't work out how to get it.我想要的只是纬度/经度,但我不知道如何得到它。 I tried pushing the stringified data into an array, I tried writing an function that assigned the stringified data to a variable, and I'm not getting it.我尝试将字符串化数据推送到一个数组中,我尝试编写一个将字符串化数据分配给变量的函数,但我没有得到它。

It's probably something really obvious that I'm missing.我错过了一些很明显的东西。 If you could unblind me I'd sure appreciate it.如果你能揭开我的双眼,我一定会很感激的。

Here's what shows up in the console log.这是控制台日志中显示的内容。

"type":"FeatureCollection","query":["1600","pennsylvania","avenue","washington","dc"],"features":[{"id":"address.1048737153875776","type":"Feature","place_type":["address"],"relevance":0.90963,"properties":{"accuracy":"rooftop"},"text":"Pennsylvania Avenue Southeast","place_name":"1600 Pennsylvania Avenue Southeast, Washington, District of Columbia 20003, United States","matching_place_name":"1600 Pennsylvania Avenue Southeast, Washington, DC 20003, United States","center":[-76.982015,38.879235],"geometry":{"type":"Point","coordinates":[-76.982015,38.879235]},"address":"1600","context":[{"id":"neighborhood.2918372884011750","text":"Capitol Hill"},{"id":"postcode.12587193810898840","text":"20003"},{"id":"place.2915387490246050","wikidata":"Q61","text":"Washington"},{"id":"region.14064402149979320","short_code":"US-DC","wikidata":"Q3551781","text":"District of Columbia"},{"id":"country.14135384517372290","wikidata":"Q30","short_code":"us","text":"United States"}]}],"attribution":"NOTICE: "type":"FeatureCollection","query":["1600","pennsylvania","avenue","washington","dc"],"features":[{"id":"address.1048737153875776", "type":"Feature","place_type":["address"],"relevance":0.90963,"properties":{"accuracy":"rooftop"},"text":"Pennsylvania Avenue Southeast","place_name ":"1600 Pennsylvania Avenue Southeast, Washington, District of Columbia 20003, United States","matching_place_name":"1600 Pennsylvania Avenue Southeast, Washington, DC 20003, United States","center":[-76.982015,38.879235],"几何":{"type":"Point","坐标":[-76.982015,38.879235]},"address":"1600","context":[{"id":"neighborhood.2918372884011750","text ":"国会山"},{"id":"postcode.12587193810898840","text":"20003"},{"id":"place.2915387490246050","wikidata":"Q61","text" :"Washington"},{"id":"region.14064402149979320","short_code":"US-DC","wikidata":"Q3551781","text":"哥伦比亚特区"},{"id" :"country.14135384517372290","wikidata":"Q30","short_code":"us","text":"United States"}]}],"attribution":"注意: © 2022 Mapbox and its suppliers. © 2022 Mapbox 及其供应商。 All rights reserved.版权所有。 Use of this data is subject to the Mapbox Terms of Service ( https://www.mapbox.com/about/maps/ ).使用此数据受 Mapbox 服务条款 ( https://www.mapbox.com/about/maps/ ) 的约束。 This response and the information it contains may not be retained.此回复及其包含的信息可能不会保留。 POI(s) provided by Foursquare."}由 Foursquare 提供的 POI。"}

data is an object. data是一个对象。 And assuming you want the lat/lng information in the coordinates array you can navigate it like this.假设您想要coordinates数组中的lat/lng信息,您可以像这样导航它。

 const data={type:"FeatureCollection",query:["1600","pennsylvania","avenue","washington","dc"],features:[{id:"address.1048737153875776",type:"Feature",place_type:["address"],relevance:.90963,properties:{accuracy:"rooftop"},text:"Pennsylvania Avenue Southeast",place_name:"1600 Pennsylvania Avenue Southeast, Washington, District of Columbia 20003, United States",matching_place_name:"1600 Pennsylvania Avenue Southeast, Washington, DC 20003, United States",center:[-76.982015,38.879235],geometry:{type:"Point",coordinates:[-76.982015,38.879235]},address:"1600",context:[{id:"neighborhood.2918372884011750",text:"Capitol Hill"},{id:"postcode.12587193810898840",text:"20003"},{id:"place.2915387490246050",wikidata:"Q61",text:"Washington"},{id:"region.14064402149979320",short_code:"US-DC",wikidata:"Q3551781",text:"District of Columbia"},{id:"country.14135384517372290",wikidata:"Q30",short_code:"us",text:"United States"}]}]}; // Access the first element of the `features` array and // find the `geometry` property. Destructure the `coordinates` // property (also and array) from it. const { coordinates } = data.features[0].geometry; // And then destructure the `lat/lng` values from // that array const [lat, lng] = coordinates; console.log(lat, lng);

Additional documentation附加文件

The response.json() is a JavaScript object. response.json() 是一个 JavaScript 对象。 So something like this should work for you.所以像这样的东西应该适合你。 I'm just logging to the console here, but you could save the data to a database via an ajax call or dynamically create a html table to view the details.我只是在这里登录到控制台,但是您可以通过 ajax 调用将数据保存到数据库中,或者动态创建一个 html 表来查看详细信息。

var response = fetch(myURL)
    .then(response => {
        responseObject = response.json();
        let features = responseObject.features;        
        for (let i = 0; i < features.length; i++)
        {
            let coords = responseObject.features[i].geometry.coordinates;
            console.log(`${features.id} Lat = ${coords[0]} Long = ${coords[1]}`);
        }
    });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM