gpt4 book ai didi

Java Gson - 帮我解析我的 JSON

转载 作者:太空狗 更新时间:2023-10-29 16:09:27 25 4
gpt4 key购买 nike

我是 GSON 的新手,我真的很难解析这些数据:

{
"0":{
"county":"Carlow",
"coords":{
"lat":"52.72465",
"lng":"-6.92963"
}
},
"1":{
"county":"Cavan",
"coords":{
"lat":"53.9011",
"lng":"-7.34436"
}
},
"2":{
"county":"Clare",
"coords":{
"lat":"52.73629",
"lng":"-8.97583"
}
},
"3":{
"county":"Cork",
"coords":{
"lat":"51.78823",
"lng":"-8.46771"
}
},
"4":{
"county":"Dublin North County",
"coords":{
"lat":"51.78823",
"lng":"-6.174316"
}
},
"5":{
"county":"Dublin South County",
"coords":{
"lat":"53.272605",
"lng":"-6.166077"
}
},
"6":{
"county":"Dublin 1",
"coords":{
"lat":"53.62798",
"lng":"-8.188635"
}
},
"7":{
"county":"Dublin 2",
"coords":{
"lat":"53.352996",
"lng":"-6.24637"
}
},
"8":{
"county":"Dublin 3",
"coords":{
"lat":"53.338166",
"lng":"-6.336515"
}
},
"9":{
"county":"Dublin 4",
"coords":{
"lat":"53.368377",
"lng":"-6.18873"
}
},
"10":{
"county":"Dublin 5",
"coords":{
"lat":"53.311391",
"lng":"-6.219978"
}
},
"11":{
"county":"Dublin 6",
"coords":{
"lat":"53.385938",
"lng":"-6.193697"
}
},
"12":{
"county":"Dublin 7",
"coords":{
"lat":"53.349219",
"lng":"-6.289244"
}
},
"13":{
"county":"Dublin 8",
"coords":{
"lat":"53.350221",
"lng":"-6.282517"
}
},
"14":{
"county":"Dublin 9",
"coords":{
"lat":"53.351576",
"lng":"-6.284523"
}
},
"15":{
"county":"Dublin 10",
"coords":{
"lat":"53.3866058",
"lng":"-6.23542"
}
},
"16":{
"county":"Dublin 11",
"coords":{
"lat":"53.342557",
"lng":"-6.358278"
}
},
"17":{
"county":"Dublin 12",
"coords":{
"lat":"53.34968",
"lng":"-6.24723"
}
},
"18":{
"county":"Dublin 13",
"coords":{
"lat":"53.33d3918"
}
},
"19":{
"county":"Dublin 15",
"coords":{
"lat":"53.382076",
"lng":"-6.366707"
}
},
"20":{
"county":"Dublin 16",
"coords":{
"lat":"53.382076",
"lng":"-6.366707"
}
},
"21":{
"county":"Dublin 17",
"coords":{
"lat":"53.310775",
"lng":"-6.242981"
}
},
"22":{
"county":"Dublin 18",
"coords":{
"lat":"53.407485",
"lng":"-6.205215"
}
},
"23":{
"county":"Dublin 20",
"coords":{
"lat":"53.407485",
"lng":"-6.205215"
}
},
"24":{
"county":"Dublin 22",
"coords":{
"lat":"53.355403",
"lng":"-6.371452"
}
},
"25":{
"county":"Dublin 24",
"coords":{
"lat":"53.299391",
"lng":"-6.390263"
}
},
"26":{
"county":"Galway",
"coords":{
"lat":"53.17477",
"lng":"-9.03625"
}
},
"27":{
"county":"Kerry",
"coords":{
"lat":"52.1554",
"lng":"-9.69269"
}
},
"28":{
"county":"Kildare",
"coords":{
"lat":"53.159829",
"lng":"-6.908998"
}
},
"29":{
"county":"Kilkenny",
"coords":{
"lat":"52.54296",
"lng":"-7.23175"
}
},
"30":{
"county":"Laois",
"coords":{
"lat":"52.88571",
"lng":"-7.31689"
}
},
"31":{
"county":"Lietrim",
"coords":{
"lat":"53.88815",
"lng":"-8.07495"
}
},
"32":{
"county":"Limerick",
"coords":{
"lat":"52.55465",
"lng":"-8.65448"
}
},
"33":{
"county":"Longford",
"coords":{
"lat":"53.61695",
"lng":"-7.77832"
}
},
"34":{
"county":"Louth",
"coords":{
"lat":"53.92699",
"lng":"-6.53893"
}
},
"35":{
"county":"Mayo",
"coords":{
"lat":"53.74546",
"lng":" -9.29169"
}
},
"36":{
"county":"Meath",
"coords":{
"lat":"53.45371",
"lng":" -6.7923"
}
},
"37":{
"county":"Monaghan",
"coords":{
"lat":"54.13831",
"lng":"-6.97083"
}
},
"38":{
"county":"Offaly",
"coords":{
"lat":"53.15501",
"lng":"-7.74261"
}
},
"39":{
"county":"Roscommon",
"coords":{
"lat":"53.5762",
"lng":" -8.19855"
}
},
"40":{
"county":"Sligo",
"coords":{
"lat":"54.16887",
"lng":"-8.47321"
}
},
"41":{
"county":"Tipperary",
"coords":{
"lat":"52.36386",
"lng":"-8.16833"
}
},
"42":{
"county":"Waterford",
"coords":{
"lat":"52.10988",
"lng":"-7.12189"
}
},
"43":{
"county":"Westmeath",
"coords":{
"lat":"53.34727",
"lng":"-7.46521"
}
},
"44":{
"county":"Wexford",
"coords":{
"lat":"52.2278",
"lng":"-6.48193"
}
},
"45":{
"county":"Wicklow",
"coords":{
"lat":"53.08286",
"lng":"-6.07904"
}
}
}

这是我目前所知道的,但我认为这是错误的...

public class GPSObject {
private String county;
private Coords coords;
GPSObject() {
// no-args constructor
}
public class Coords{
public String lat;
public String lng;
public Coords() {
// No args constructor for B
}
}
}


Gson gson=new Gson();
GPSObject obj2=gson.fromJson(response,GPSObject.class);

这是我遇到的一个错误:

> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763): FATAL
> EXCEPTION: main 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763):
> java.lang.RuntimeException: No-args
> constructor for class
> ie.clarity.ShowJobsOnMap$GPSObject
> does not exist. Register an
> InstanceCreator with Gson for this
> type to fix this problem. 07-08
> 09:26:08.731:
> ERROR/AndroidRuntime(26763): atcom.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763): at
> com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763): at
> com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:41)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763): at
> com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763): at
> com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)

谁能给我指出正确的方向?

非常感谢,

最佳答案

我尝试在不使用 GSON 的情况下解析此 JSON,并且能够解析此 JSON 字符串。请也尝试将此作为替代解决方案。我正在详细说明用于解析此字符串的代码 fragment :

for(int i=0; i<46; i++){
try {
JSONObject jsonObject = new JSONObject(<string to be parsed>);
String first = jsonObject.getString(Integer.toString(i));
JSONObject jsonObject1 = new JSONObject(first);
String country = jsonObject1.getString("county");
System.out.println("country="+country);
String cod = jsonObject1.getString("coords");
JSONObject jsonObject2 = new JSONObject(cod);
String lat = jsonObject2.getString("lat");
System.out.println("lat="+lat);
String lng = jsonObject2.getString("lng");
System.out.println("lng="+lng);
} catch (JSONException e) {
e.printStackTrace();
}
}

关于Java Gson - 帮我解析我的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com