-2
我想使用GSON來解析類似下面的json。請指導如何使用GSON實現此操作,因爲student字段用作對象以及數組,應該如何我定義了我的pojo以及如何解析這種類型的json。具有相同名稱但不同類型的gson字段,例如對象和數組
{
"school": [
{
"student": {
"name": "Rose",
"address": "some address"
},
"age": "15",
"section": "A"
},
{
"student": [
{
"name": "David",
"address": "Some place"
}
],
"age": "14",
"section": "B"
}
]
}
Gson gson = new Gson();
JSONArray jsonArray = response.getJSONArray("school");
Type listType = new TypeToken<ArrayList<School>>(){}.getType();
listSchool = gson.fromJson(jsonArray.toString(), listType);
越來越com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException:預期BEGIN_ARRAY但BEGIN_OBJECT例外
Pojo我創建了,但是在解析時出現錯誤com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望BEGIN_ARRAY,但是BEGIN_OBJECT Gson gson = new Gson(); JSONArray jsonArray = response.getJSONArray(「school」); 類型listType = new TypeToken>(){}。getType(); listSchool = gson.fromJson(jsonArray.toString(),listType); –
Nobdore
Gson gson = new Gson(); JSONArray jsonArray = response.getJSONArray(「school」); 類型listType = new TypeToken>(){}。getType(); listSchool = gson.fromJson(jsonArray.toString(),listType); –
Nobdore