1
希望有從別人放在這裏一個簡單的解決方案。我知道有類似的問題,但我似乎無法修改它們與我的問題的工作。解析JSON與GSON不POJO?
我試圖解析對該JSON響應「的formatted_address」字符串:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Google Building 42",
"short_name" : "Google Bldg 42",
"types" : [ "premise" ]
},
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Google Bldg 42, 1600 Amphitheatre Pkwy, Mountain
View, CA 94043, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.42198310000001,
"lng" : -122.0853195
},
"southwest" : {
"lat" : 37.4214139,
"lng" : -122.0860042
}
},
"location" : {
"lat" : 37.4216548,
"lng" : -122.0856374
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4230474802915,
"lng" : -122.0843128697085
},
"southwest" : {
"lat" : 37.4203495197085,
"lng" : -122.0870108302915
}
}
},
"place_id" : "ChIJPzxqWQK6j4AR3OFRJ6LMaKo",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
當以前使用GSON我能夠解析我的結果馬上使用:
Gson gson = new Gson();
JsonArray body = gson.fromJson(ResultString, JsonArray.class);
System.out.println(body.get(0).getAsJsonObject().get("elementnamehere").getAsString());
的主要的區別是我不能把這個結果放入JsonArray體。相反,(我相信,因爲它嵌套數據),我必須使它一個JSONObject,但沒有得到空,我不能解析它爲我的生活。任何簡單的方法來做到這一點,而不是做一個POJO或響應類?如果不是有人可以解釋如何/爲什麼這樣做,像這樣:
Parsing nested JSON data using GSON
工作就像一個魅力!不勝感激。 – Tevor