我正在从mapbox返回一个要素集合。 JSON如下所示:
{"type":"FeatureCollection","query":["17","green"],"features":[{"id":"address.8882296350520732","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"rooftop"},"text":"Green Street","place_name":"17 Green Street, Brookline, Massachusetts 02446, United States","center":[-71.121411,42.343136],"geometry":{"type":"Point","coordinates":[-71.121411,42.343136]},"address":"17","context":[{"id":"postcode.8198763973790210","text":"02446"},{"id":"place.7864891969924050","wikidata":"Q49142","text":"Brookline"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.2730621404747834","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"rooftop"},"text":"Greenough Circle","place_name":"17 Greenough Circle, Brookline, Massachusetts 02445, United States","center":[-71.125244,42.334561],"geometry":{"type":"Point","coordinates":[-71.125244,42.334561]},"address":"17","context":[{"id":"postcode.7679295126168220","text":"02445"},{"id":"place.7864891969924050","wikidata":"Q49142","text":"Brookline"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.4738306377365208","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"interpolated"},"text":"Greenleaf Street","place_name":"17 Greenleaf Street, Boston, Massachusetts 02115, United States","center":[-71.090477,42.338387],"geometry":{"type":"Point","coordinates":[-71.090477,42.338387],"interpolated":true},"address":"17","context":[{"id":"neighborhood.295553","text":"Fenway"},{"id":"postcode.11169253348388930","text":"02115"},{"id":"place.9391334652012190","wikidata":"Q100","text":"Boston"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.2688910343185888","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"point"},"text":"Greenwich Street","place_name":"17 Greenwich Street, Roxbury Crossing, Massachusetts 02120, United States","center":[-71.083751,42.335527],"geometry":{"type":"Point","coordinates":[-71.083751,42.335527]},"address":"17","context":[{"id":"neighborhood.295329","text":"South End"},{"id":"postcode.7229336676579040","text":"02120"},{"id":"place.2024861849164830","wikidata":"Q20138","text":"Roxbury Crossing"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.4057524105398816","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"rooftop"},"text":"Greenwich Park","place_name":"17 Greenwich Park, Boston, Massachusetts 02118, United States","center":[-71.080331,42.342747],"geometry":{"type":"Point","coordinates":[-71.080331,42.342747]},"address":"17","context":[{"id":"neighborhood.294804","text":"Back Bay"},{"id":"postcode.8640649655199430","text":"02118"},{"id":"place.9391334652012190","wikidata":"Q100","text":"Boston"},{"id":"region.6776276020561540","short_code":"US-MA","wikidata":"Q771","text":"Massachusetts"},{"id":"country.9053006287256050","short_code":"us","wikidata":"Q30","text":"United States"}]}],"attribution":"NOTICE: © 2019 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."}
JSON输出包含FeatureCollection。在要素数组中,每个要素都有一个属性“ place_name”,该属性给出列出的所有地点的完整地址。这就是我要访问的。
每当我将此JSON读入我的android应用程序并尝试将其转换为FeatureCollection时,我都会发现“ place_name”属性不是com.mapbox.geojson.Feature的一部分。我正在使用以下内容将JSON转换为FeatureCollection:
FeatureCollection.fromJson(response);
响应是上面显示的返回的JSON。我的问题是:如果MapBox没有将其作为com.mapbox.geojson.Feature的一部分,该如何访问该属性?我还应该使用Mapbox附带的其他一些类吗?
答案 0 :(得分:0)
您可以在创建集合之前将该属性添加到功能中:
Feature feature = new Feature().fromJson(response);
feature.addStringProperty("place_name", "17 Green Street, Brookline, Massachusetts 02446, United States");
FeatureCollection.fromFeature(feature);
然后,用于查询:
feature.getProperties()
获取与该功能关联的所有属性,或者feature.getStringProperty("place_name")
获取特定的属性。
看看功能api。