[英]Parsing GSON Google Places API
I'm trying to parse a json to my java object, but I'm getting error from gson. 我正在尝试将json解析为我的java对象,但是我从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 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 谢谢
You are using your Gson instance wrong. 您使用的Gson实例错误。 It should be 它应该是
gson.toJson(ritorno);
you do NOT need to call ritorno.toString()
你不需要调用ritorno.toString()
Gson can directly serialize from an object. Gson可以直接从对象进行序列化。
toString()
has actually nothing to do with json, or serialization. toString()
实际上与json或序列化无关 。
You should implement your own toString() method for your classes to have better readability for debugging. 您应该为自己的类实现自己的toString()方法,以提高调试的可读性。 It has nothing to do with serializing your object to a String. 它与将对象序列化为String无关。
Gson is extremely to use and if you are not worried about performances then it is a good choice. Gson非常有用,如果您不担心表演,那么它是一个不错的选择。 Otherwise there is (really) faster (but slightly less obvious to use or configure) libraries like : Jackson-Json 否则,有(确实)更快(但使用或配置不那么明显)的库,例如: Jackson-Json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.