java 无法使用 Gson 解析 Json 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17650419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to parse Json array using Gson
提问by Tomer
I have this json array:
我有这个 json 数组:
[
{
"id":18,
"city":"??????",
"street":"??????? 1",
"zipcode":121209,
"state":"IL",
"lat":32.158138,
"lng":34.807838
},
{
"id":19,
"city":"??????",
"street":"??? ??? 1",
"zipcode":76812,
"state":"IL",
"lat":32.161041,
"lng":34.810410
}
]
And i have this class to hold the data:
我有这个类来保存数据:
public class MapData {
private int id;
private String city;
private String street;
private String state;
private int zipcode;
private double lat;
private double lng;
public MapData(int id, String city, String street, String state,
int zipcode, double lat, double lng) {
this.id = id;
this.city = city;
this.street = street;
this.state = state;
this.zipcode = zipcode;
this.lat = lat;
this.lng = lng;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
}
I'm trying to convert the json into a List of MapData objects:
我正在尝试将 json 转换为 MapData 对象列表:
Type type = new TypeToken<List<MapData>>(){}.getType();
return gson.fromJson(jsonString, type);
But i get this error:
但我收到此错误:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.deliveries.models.MapData
What am i doing wrong?
我究竟做错了什么?
回答by Kevin Bowersox
I suspect that the method calling fromJson
is returning the wrong type. It should return a List<MapData>
instead of MapData
.
我怀疑方法调用fromJson
返回了错误的类型。它应该返回 aList<MapData>
而不是MapData
。
Something like:
就像是:
public static List<MapData> getData(){
Gson gson = new Gson();
String jsonString = "[{\"id\":18,\"city\":\"test\",\"street\":\"test 1\",\"zipcode\":121209,\"state\":\"IL\",\"lat\":32.158138,\"lng\":34.807838},{\"id\":19,\"city\":\"test\",\"street\":\"1\",\"zipcode\":76812,\"state\":\"IL\",\"lat\":32.161041,\"lng\":34.810410}]";
Type type = new TypeToken<List<MapData>>(){}.getType();
return gson.fromJson(jsonString, type);
}
I have a fully working example of your issue in this Gist
我在这个Gist 中有一个关于你的问题的完整示例
回答by Alireza Masali
string Json="some Json String";
JavaScriptSerializer Js = new JavaScriptSerializer();
MapData ObjMapDat = new MapData ();
ObjMapDat =Js.Deserialize<MapData>(Json);