4

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>例外が発生します。

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";
}

ありがとう

4

1 に答える 1

5

は、JSON 配列でdealはなく、JSON オブジェクトです。したがって、例外です。参考までに、JSON 配列は次のようになります。

"deal" : [{"attr" : "value"}, {"attr" : "value"}]
于 2013-01-07T03:26:27.013 に答える