我試圖反序列化一個JSON到我的對象,但我得到一個NullPointerException
。搜索後,我發現這個問題可能在數據結構中。反序列化JSON以反對Gson
你能幫我創建一個合適的班級,或者發現我的錯誤嗎?
[
{
"id": "54",
"brand": "Cooper",
"model": "Discoverer H\/T Plus",
"d": "13",
"w": "7",
"h": "0",
"comment": "test1",
"time_add": "2013-11-14 12:42:47",
"imgs": [
{
"id": "28",
"reused_id": "54",
"path_big": "path5",
"path_small": "",
"time_add": "0000-00-00 00:00:00"
}
]
},
{
"id": "55",
"brand": "Barum",
"model": "Bravuris",
"d": "13",
"w": "7",
"h": "0",
"comment": "ooooooopll",
"time_add": "2013-11-14 12:43:55",
"imgs": [
{
"id": "29",
"reused_id": "55",
"path_big": "path5",
"path_small": "",
"time_add": "0000-00-00 00:00:00"
}
]
},
{
"id": "56",
"brand": "Kumho",
"model": "Kumho KH17",
"d": "19",
"w": "185",
"h": "50",
"comment": "bugaga",
"time_add": "2013-11-14 13:14:58",
"imgs": [
{
"id": "30",
"reused_id": "56",
"path_big": "path5",
"path_small": "",
"time_add": "0000-00-00 00:00:00"
}
]
},
{
"id": "57",
"brand": "Barum",
"model": "Bravuris",
"d": "13",
"w": "7",
"h": "0",
"comment": "",
"time_add": "2013-11-14 13:32:11",
"imgs": [
{
"id": "31",
"reused_id": "57",
"path_big":path5",
"path_small": "",
"time_add": "0000-00-00 00:00:00"
}
]
},
{
"id": "58",
"brand": "Barum",
"model": "Bravuris",
"d": "13",
"w": "7",
"h": "0",
"comment": "",
"time_add": "2013-11-14 13:33:13",
"imgs": [
{
"id": "32",
"reused_id": "58",
"path_big": "path5",
"path_small": "",
"time_add": "0000-00-00 00:00:00"
}
]
}
]
這是我試圖填補了類:
public class Tyres {
public Tyres() {
}
public String id;
public String brand;
public String model;
public String d;
public String w;
public String h;
public String comment;
public String time_add;
public ArrayList<Images> imgs;
public Map<String, String> getInfo() {
Map<String, String> result = new HashMap<String, String>();
result.put("id", id);
result.put("brand", brand);
result.put("model", model);
result.put("d", d);
result.put("w", w);
result.put("h", h);
result.put("comment", comment);
result.put("time_add", time_add);
return result;
}
public ArrayList<Map<String, String>> getImgsInfo() {
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
Map<String, String> imgInfo;
for(Images img: imgs) {
imgInfo = img.getInfo();
result.add(imgInfo);
}
return result;
}
}
public class Images {
public Images() {
}
public String id;
public String reused_id;
public String path_big;
public String path_small;
public String time_add;
public Map<String, String> getInfo() {
Map<String, String> result = new HashMap<String, String>();
result.put("id", id);
result.put("reused_id", reused_id);
result.put("path_big", path_big);
result.put("path_small", path_small);
result.put("time_add", time_add);
return result;
}
}
和遵循的是我如何努力來初始化對象:
private void history_to_Tyres(String historyJSON) {
Gson gson = new Gson();
Type ttype = (Type) new ArrayList<Tyres>();
try {
tyres = gson.fromJson(historyJSON, ttype);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "uncasted", Toast.LENGTH_SHORT).show();
}
}
,但我有一個NullPointerException
當我嘗試撥打電話tyres
(輪胎是一個班級字段)
您是否需要手動解析數據?你爲什麼不直接使用「映射」? – TeeTracker
感謝您的建議,我在這篇文章中找到了解決方案(聲明所有getters&setters之後): http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array – Sild