0
我与后续的JSON对象的工作,我从谷歌获取Places API的获得2个值由大JSON对象
{
"debug_info":[
],
"html_attributions":[
],
"result":{
"address_components":[
{
"long_name":"109",
"short_name":"109",
"types":[
"street_number"
]
},
{
"long_name":"Torstraße",
"short_name":"Torstraße",
"types":[
"route"
]
},
{
"long_name":"Mitte",
"short_name":"Mitte",
"types":[
"sublocality",
"political"
]
},
{
"long_name":"Mitte",
"short_name":"Mitte",
"types":[
"sublocality",
"political"
]
},
{
"long_name":"Berlin",
"short_name":"Berlin",
"types":[
"locality",
"political"
]
},
{
"long_name":"Berlin",
"short_name":"Berlin",
"types":[
"administrative_area_level_1",
"political"
]
},
{
"long_name":"Germany",
"short_name":"DE",
"types":[
"country",
"political"
]
},
{
"long_name":"10119",
"short_name":"10119",
"types":[
"postal_code"
]
}
],
"adr_address":"\u003cspanclass=\"street-address\"\u003eTorstraße109\u003c/span\u003e,\u003cspanclass=\"postal-code\"\u003e10119\u003c/span\u003e\u003cspanclass=\"locality\"\u003eBerlin\u003c/span\u003e,\u003cspanclass=\"country-name\"\u003eGermany\u003c/span\u003e",
"formatted_address":"Torstraße109,10119Berlin,Germany",
"geometry":{
"location":{
"lat":52.5300431,
"lng":13.40297
}
},
"icon":"http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id":"18c00ced59b4a7fb929dde10a01169517666eec5",
"name":"Torstraße109",
"reference":"CpQBiQAAAGrYOH9GhUIR5_9XJm8YPZYoudNVoYeIWwD1-zFzjdUp2eKujIt85bUM78FWiY9OgGm2pPoxnCjE5EMhXNz9hiPcLLybacpdSQ10x0mO8UQNs1Mj-EyjGMfBaowMSAxeye_2aDvCyJEk5JAkzTkqXenGi60Dx24o5zKTH1nt1yDBJ3BlKb7Uaas6dBTz2GoqKBIQNFg_NmrCnRGgLxw5VEvtghoU6fl4owiC-5mNZ5RCjS976-S1fyQ",
"types":[
"street_address"
],
"url":"https://maps.google.com/maps/place?q=Torstra%C3%9Fe+109,+10119+Berlin,+Germany&ftid=0x47a851e486a78201:0xec18bf6093c5c499",
"vicinity":"Mitte"
},
"status":"OK"
}
,我想拉的唯一场所,它是纬度,经度和formated_address 。
使用杰克森最有效的方法是什么?
我知道我可以编写一个自定义的反序列化器,但是之后我必须多次调用jsonParser.nextToken。
什么是更好的方式去解决这个问题?
编辑
我能拿出一个目前最好的是这个
public class LocationDeserializer extends JsonDeserializer<Location> {
@Override
public Location deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
Location location = new Location();
int len = 100;
for (int i = 0; i < len; ++i) {
String currentName = jsonParser.getCurrentName();
if (currentName != null) {
if (jsonParser.getCurrentName().equals("formatted_address")) {
location.setDescription(jsonParser.getText());
} else if (jsonParser.getCurrentName().equals("lat")) {
location.setLatitude(jsonParser.getDoubleValue());
} else if (jsonParser.getCurrentName().equals("lng")) {
location.setLongitude(jsonParser.getDoubleValue());
break;
}
}
jsonParser.nextValue();
}
return location;
}
}
好吧,我用杰克逊库等反序列化/序列化和它运作良好。我认为这是一致的好。另外,我可以用下面的行来减少大量的锅炉板代码: Location location = objectMapper.readValue(httpURLConnection.getInputStream(),Location.class); 不管JSONObject是否必须迭代对象? – jiduvah
显然,您可以调整我上面提供给您正在使用的Jackson Library的内容。我不熟悉它,所以我不能告诉你如何,但想法是相同的,以获得持有的特定JSON节点访问值,而不是遍历整个JSON响应 – Lefteris
你怎么知道如果你不清楚图书馆是否明显? – jiduvah