1
我JSON響應(命名爲jsonResultAsText):JSON數組到Java類(GSON反序列化)
{
"Title": "Hi all",
"IsSecret": false,
"Agents": [
{
"ID": 3,
"Name": "John F"
},
{
"ID": 7,
"Name": "Jack D"
}
]
}
我想使用谷歌的GSON庫反序列化。
我有類,如:
public class JsonResponse {
public String Title;
public boolean IsSecret;
public List<Agent> Agents;
}
public class Agent {
public int ID;
public String Name;
}
我想JSON數據到JsonResponse類。
這是我努力的實現代碼:
JsonResponse response =
new Gson().fromJson(jsonResultAsText, JsonResponse.class);
但我的Android應用程序給出了經典的錯誤:不幸的是,< APP_NAME>已停止。
我知道有很多關於通過gson類序列化/反序列化的例子,但我找不到類似的問題/例子。
在此先感謝。
您發佈的代碼沒有問題,並且是使用Gson反序列化您的JSON的正確方法。你的問題在於別處,或者你的JSON不是你認爲的那樣。 –
你看過logcat中的堆棧跟蹤嗎?這應該讓你知道發生了什麼。 –
@VivinPaliath LogCat沒有信息。 – JustWork