Reputation: 1712
I'm trying to parse a json to my java object, but I'm getting error from gson.
The error is:
com.google.gson.JsonParseException: Expecting object found: "{ \"debug_info\" : [], \"html_attributions\" : [], \"results\" : [ { \"geometry\" : { \"location\" : { \"lat\" : 53.330661, \"lng\" : -6.265253 } }, \"icon\" : \"http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png\", \"id\" : \"b50995ee5107706386c43d562fc614dc9db57937\", \"name\" : \"Lower Deck\", \"rating\" : 3.5, \"reference\" : \"CnRoAAAATUQrAt8LAzje_32Uzm5jklTmhsYA_orKtp9DIO_-kmCTU7DsHkNBae3aY9dLusdqJaSGwdj6G_-LpqbKWIi5r0RjcJWHljxCex8wI9UMO93uqSpr63S6qyNjJdw01nGEl1LLtbtz4VRGuKdEAl6sShIQEeM3-QnEjeoO7lEWZBYQQBoU0TOKwurVvTs565wKYPLQNmkLF5w\", \"types\" : [ \"bar\", \"establishment\" ], \"vicinity\" : \"1 Portobello Harbour, Dublin\" }, { \"geometry\" : { \"location\" : { \"lat\" : 53.332361, \"lng\" : -6.275473 } }, \"icon\" : \"http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png\", \"id\" : \"8e8f338164d20d4ad7d943db980ce62d0325c2b5\", \"name\" : \"The Headline Bar\", \"rating\" : 3.6, \"reference\" : \"CnRvAAAAvlARk-Q-08T9kuvY_mp90vn10jf84TDNKymDtVyEYvt1wg7TEJyaqGF_R6zDGkXBoKSOEfovqm-A8w42OlOa1yAE-nMdGVgR_EKJKu5HHdzUmKlkFoPqcJxbJpFAblqCMz_ClpbwMEMtFNLA_hZidRIQVCg4_6vqhJuSDSqtbIG2zxoUeEGHiUYqFk2e_aB18dqFSKx_E5Y\", \"types\" : [ \"bar\", \"establishment\" ], \"vicinity\" : \"118 S Circular Rd, Crumlin, Dublin\" } ], \"status\" : \"OK\"}"
Then my classes are the following:
GoogleMapper
@SerializedName("debug_info")
private List<String> debug_info;
@SerializedName("html_attributions")
private List<String> html_attributions;
@SerializedName("next_page_token")
private String next_page_token;
@SerializedName("results")
private List<Results> results;
@SerializedName("status")
private String status;
Results
@SerializedName("geometry")
private Geometry geometry;
@SerializedName("icon")
private String icon;
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("photos")
private Photos photos;
@SerializedName("rating")
private Double rating;
@SerializedName("reference")
private String reference;
@SerializedName("types")
private List<String> types;
@SerializedName("vicinity")
private String vicinity;
Geometry
@SerializedName("location")
private Location location;
Location
@SerializedName("lat")
private Double lat;
@SerializedName("lng")
private Double lng;
Photos
@SerializedName("height")
private int height;
@SerializedName("width")
private int width;
@SerializedName("html_attributions")
private List<String> html_attributions;
@SerializedName("photo_reference")
private String photo_reference;
And finally, I'm trying to do the following code:
Gson gson = new GsonBuilder().serializeNulls().create();
String json = gson.toJson(retorno.toString());
GoogleMapper mapper = gson.fromJson(json, GoogleMapper.class);
Please, anyone can help me? Thanks
Upvotes: 2
Views: 1041
Reputation: 3367
You are using your Gson instance wrong. It should be
gson.toJson(ritorno);
you do NOT need to call ritorno.toString()
Gson can directly serialize from an object.
toString()
has actually nothing to do with json, or serialization.
You should implement your own toString() method for your classes to have better readability for debugging. It has nothing to do with serializing your object to a String.
Gson is extremely to use and if you are not worried about performances then it is a good choice. Otherwise there is (really) faster (but slightly less obvious to use or configure) libraries like : Jackson-Json
Upvotes: 1