我正在尝试解析google places api的json结果。我使用http方法调用它,并得到字符串形式的json结果,我如何解析它?我看了医生的报告,在互联网上浏览了两个博客(一个使用google api库,另一个似乎已经过时了),但没有帮到我
我正在使用google的标准java JSONobject和Gson,但我找不到一个好的方法来解析它(我称之为搜索位置方法),你有什么建议吗?谢谢你
嗨,很抱歉忘记显示JSON响应:
{
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.ca/\"\u003eYellowPages.ca\u003c/a\u003e"
],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 45.39318670,
"lng" : -75.68352930
},
"viewport" : {
"northeast" : {
"lat" : 45.40102170,
"lng" : -75.66752190
},
"southwest" : {
"lat" : 45.38535060,
"lng" : -75.69953670
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "6a3f8ef9aae04c8a23c80ab814e8c75a8b685292",
"name" : "Old Ottawa South",
"reference" : "adsadada",
"types" : [ "neighborhood", "political" ],
"vicinity" : "Ottawa"
},
{
"geometry" : {
"location" : {
"lat" : 45.394630,
"lng" : -75.6837250
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/movies-71.png",
"id" : "59519a28667351790456ab80aba982347546b2f6",
"name" : "Mayfair Theatre",
"rating" : 4.10,
"reference" : "dfdsf",
"types" : [ "movie_theater", "establishment" ],
"vicinity" : "1074 Bank Street, Ottawa"
}
]
}我想要获取结果部分(为了简单起见,让我们只说结果的第一项),我所做的是:
JSONObject jso = new JSONObject(response);
String result = jso.getJSONArray("results").getJSONObject(0).toString();
JsonPlacesResult d = new Gson().fromJson(result, JsonPlacesResult.class);
where JsonPlacesResult is
class JsonPlacesResult{
private JsonPlaces jsonPlaces;
public JsonPlaces getJsonPlaces(){return jsonPlaces;}
}
class JsonHtml{
private String html_attribution;
}
class JsonPlaces{
private Geometry geo;
private String icon;
private String id;
private String name;
private String rating;
private String reference;
private String types;
private String vicinity;
}
class Geometry{
private JsonLocation loc;
}
class JsonLocation{
private String lat;
private String lng;
}我在d?中以空对象结束为什么会这样!我做错了什么吗?谢谢
发布于 2012-03-29 22:07:01
3个错误:1.lat和lng的类型应该是float而不是String;2.html_attributions的类型应该是List(String)而不是String;3.types的类型应该是List(String)而不是String;对不起,我的英语不太好。
https://stackoverflow.com/questions/9645184
复制相似问题