我正在使用我从google places api
检索的跟随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"
}
我想要拉出来的唯一属性是lat,lng和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;
}
}
答案 0 :(得分:-1)
为什么你没有按照它的价值拿起钥匙?
JSONObject json = new JSONObject(response);
响应是您从Google收到的回复。
然后获取你的价值观:
JSONObject address = json.getJSONObject("result");
String formattedAddress = address.getString("formatted_address");
JSONObject locationData = address.getJSONObject("geometry").getJSONObject("location");
Location location = new Location();
location.setLatitude(locationData.getDouble("lat"));
location.setLongitude(locationData.getDouble("lng"));
您需要导入
import org.json.JSONObject;