简体   繁体   中英

Converting GeoJSON object to Javascript Array

I need to convert a GeoJSON output from a server to a Javascript array; I have done some searches and they were for the 'regular' JSON outputs and not the GeoJSON. Here is the output from the Server:

{"type":"FeatureCollection","features":[{"type":"Feature","property":"blah"}, {"type":"Feature","property":"blah2"}]}

And here is what I need (note, no quotation on the 'features' variable needed):

features = [{"type":"Feature","property":"blah"}, {"type":"Feature","property":"blah2"}]}

I think I need to find the 'features' property and then loop through its objects? Thanks!

A GeoJSON object is still a JSON object (it's the first thing their documentation says).

http://geojson.org/geojson-spec.html

If you want to store the features PROPERTY of a GeoJSON object, access the property value as you would normally after conversion to a javascript object and push that property into a new variable, or use as is.

var geoObject = JSON.parse(geoJSONObject);
var features = [];

features = geoObject.features;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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