使用fetch使用json.stringify(),然后...?

发布于 2025-01-31 03:33:54 字数 1564 浏览 2 评论 0 原文

从周日开始,我已经搜索了这九种方式,但是这里有一些东西,我不知道这是什么。

我正在使用fetch从Mapbox获取一些数据:

var response = fetch(myURL)

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

当我运行它并查看控制台时,我可以看到我确实抓住了数据,因为它已记录。 (我将最后记录的内容放在最后是因为很多。)

问题是我不知道如何将信息转化为可以使用它的格式。我想要的是lat/long,但我无法弄清楚如何获得它。我尝试将弦乐化数据推入数组,尝试编写一个将弦乐化数据分配给变量的函数,但我没有得到它。

我缺少这很明显。如果您能解散我,我肯定会感谢它。

这是在控制台日志中显示的内容。

“ type”:“ featurecollection”,“ Query”:[“ 1600”,“宾夕法尼亚州”,“ Avenue”,“ Washington”,“ DC”],“特征”:[{“ ID”:“ address.1048737153875776”, “ type”:“ feature”,“ plot_type”:[address'],“相关”:0.90963,“ properties”:{“ cocuracy”:“ roftop”},“ text”:“ Pennsylvania Avenue Southeast”,“ Southeast”,“ plot_name” “:” 1600宾夕法尼亚大道东南,华盛顿,华盛顿,哥伦比亚特区,20003年,美国”,“ Matching_place_name”:“ 1600 Pennsylvania Avenue Southeast,Washington,Washington,Washington,DC 20003,美国”几何“:{” type“:”点“,”坐标:[ - 76.982015,38.879235]},“地址”:“ 1600”,“ context”:[{“ id”:{“ idegroom.291872884401111750) “:“ Capitol Hill”},{“ ID”:“ PostCode.12587193810898840”,“ Text”:“ 20003”},{“ ID”:“ plot.29153874902460246050” :“华盛顿”},{“ id”:“ region.14064402149979320”,“ short_code”:“ us-dc”,“ wikidata”:“ Q35551781”,“ Q35551781”,“文本”:“哥伦比亚区”:“哥伦比亚特区” :“ country.14135384517372290”,“ wikidata”:“ q30”,“ short_code”:“ us”,“ text”:“ united state”}]}]}],“属性”:“通知:©20222 MAPBOX及其供应商。版权所有。此数据的使用受“ MAPBox”服务条款的约束( https://www.mapbox.com/大约/地图/)。此响应及其包含的信息可能不会保留。 foursquare提供的poi。”}

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:

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: © 2022 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2025-02-07 03:33:54

数据是一个对象。并假设您需要 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);

附加文档

data is an object. And assuming you want the lat/lng information in the coordinates array you can navigate it like this.

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

仅冇旳回忆 2025-02-07 03:33:54

响应.json()是一个JavaScript对象。因此,这样的事情应该对您有用。我只是在此处登录到控制台,但是您可以通过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]}`);
        }
    });

The response.json() is a JavaScript object. 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.

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]}`);
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文