问题
Hoping there is an easy solution from someone on here. I know there are similar questions but I can't seem to modify them to work with my problem.
I am trying to parse the string for "formatted_address" in this json response:
{
"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"
}
When previously using gson I was able to parse my result right away using:
Gson gson = new Gson();
JsonArray body = gson.fromJson(ResultString, JsonArray.class);
System.out.println(body.get(0).getAsJsonObject().get("elementnamehere").getAsString());
The main difference is that I can't put this result into JsonArray body. Instead (I believe since it has nested data) I have to make it a JsonObject, but I can not parse it for the life of me without getting Null. Any easy way to do this without making a POJO or Response Classes? If not can someone explain how/why to do that like so:
Parsing nested JSON data using GSON
回答1:
You are almost there and you are correct that you need to parse JsonObject
.
JsonObject body = gson.fromJson(json, JsonObject.class);
JsonArray results = body.get("results").getAsJsonArray();
JsonObject firstResult = results.get(0).getAsJsonObject();
JsonElement address = firstResult.get("formatted_address");
System.out.println(address.getAsString());
回答2:
You can do it, but you do not have to use GSON necessarily.
You can parse a JSON as Java Objects with Jackson
. For example you can obtain: Object, List, Hashmap, Integer, String, etc. without POJO classes.
In android first add to gradle the following line:
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
next import the mapper
import com.fasterxml.jackson.databind.ObjectMapper
at last you can use the mapper passing the String JSON value, example:
HashMap<*,*> object = new ObjectMapper().readValue(body, HashMap.class);
I hope it helps you. To see more about Jackson please visit: Jackson
来源:https://stackoverflow.com/questions/46841661/parse-json-with-gson-without-pojo