2
我在解析JSON響應時遇到了一些麻煩。響應數據:com.google.gson.JsonObject無法轉換爲com.google.gson.JsonArray
{
"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>
我得到這個異常:
com.google.gson.JsonObject不能轉換到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";
}
謝謝
是的,但我的呼喚jsonObejct.getAsJsonArray(「交易」),我想找到JsonArray我有這樣的例外 – stackHunter
我不知道如何進一步分解它,你不能從你的對象中得到一個JSON數組,因爲它沒有數組。在你發佈的整個JSON中幾乎沒有數組。你可能試圖訪問'deal'的***屬性嗎? – Perception
是的,但我使用谷歌庫JSON Gson&它的提供的可能性將JSONObject轉換爲JsonArray,但我不知道爲什麼我有這個錯誤 – stackHunter