gpt4 book ai didi

json - com.google.gson.JsonObject 不能转换为 com.google.gson.JsonArray

转载 作者:行者123 更新时间:2023-12-05 01:13:05 25 4
gpt4 key购买 nike

我在解析 JSON 响应时遇到了一些麻烦。响应数据:

{
"deal": {
"categorie": {
"description": "Offres Shopping",
"idcategorie": "1",
"nom": "Shopping"
},
"conditions": "2 personne au plus",
"dateAjout": "2013-01-07T00:00:00+01:00",
"dateExp": "2013-01-31T00:00:00+01:00",
"description": "nuit dans un hotel 5 etoile",
"heurexp": "12",
"iddeal": "1",
"minutesexp": "30",
"prestataire": {
"adresse": "Qu zohour 44",
"codePostale": "12600",
"description": "Hotel 5 etoiles",
"idprestataire": "1",
"nom": "Hotel ronald",
"pays": "France",
"telephone": "99999999",
"ville": "Brest"
},
"prix": "80.0",
"prixHabituel": "200.0",
"tags": "hotel",
"titre": "Nuit 5 etoiles"
}
}

尝试将此响应解析为 List<Deal> 时我得到这个异常(exception):

com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray



这是我用于解析的代码:
if (reponse != null && !reponse.isEmpty()) {
System.out.println(reponse);

Gson g = new Gson();
JsonParser parser = new JsonParser();
JsonObject jObject = parser.parse(reponse).getAsJsonObject();
JsonArray jArray = jObject.getAsJsonArray("deal"); // here goes the Exception
for (JsonElement elem : dealArray) {
deals.add(g.fromJson(elem, Deal.class));
}

System.out.println(deals.toString());
return "success";
}

谢谢

最佳答案

那么,deal不是一个 JSON 数组,它是一个 JSON 对象。因此异常(exception)。一个 JSON 数组,作为引用,看起来更像这样:

"deal" : [{"attr" : "value"}, {"attr" : "value"}]

关于json - com.google.gson.JsonObject 不能转换为 com.google.gson.JsonArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14189439/

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