我正在尝试将json解析为我的java对象,但是我从gson中得到了错误。
错误是:
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\"}"
然后我的课程如下:
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;
结果
@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;
几何
@SerializedName("location")
private Location location;
地点
@SerializedName("lat")
private Double lat;
@SerializedName("lng")
private Double lng;
相片
@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;
最后,我尝试执行以下代码:
Gson gson = new GsonBuilder().serializeNulls().create();
String json = gson.toJson(retorno.toString());
GoogleMapper mapper = gson.fromJson(json, GoogleMapper.class);
拜托,有人可以帮我吗?
谢谢
最佳答案
您使用的Gson实例错误。它应该是
gson.toJson(ritorno);
您无需致电
ritorno.toString()
Gson可以直接从对象进行序列化。
toString()
实际上与json或序列化无关。有关良好做法的其他信息:
您应该为自己的类实现自己的toString()方法,以提高调试的可读性。它与将对象序列化为String无关。
Gson非常有用,如果您不担心表演,那么它是一个不错的选择。否则,有(确实)更快(但使用或配置起来不太明显)的库,例如:Jackson-Json
关于java - 解析GSON Google Places API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18275768/