我有我從我的Android應用程序調用和失控的字符串,這個JSON數據一個ASP.NET web服務:GSON的Android JSON parsning錯誤
{
"d": [{
"__type": "Trafikverket_API.Models.Trafiklage",
"TagGrupp": "44be2cce-3055-4d22-aaf2-00e098b0422e",
"Fran": "Gävle,Uppsala,Stockholm",
"Till": "Norrköping,Linköping",
"TrafikplatsNamn": "Nyköping C",
"VisaAnkomst": "true",
"InstalldAnkomst": "false",
"InstalldAvgang": "false",
"SparangivelseAvgang": "6",
"AnmarkningarAvgang": "SJ Regional",
"AnnonseratTagId": "249",
"SparangivelseAnkomst": "6",
"AnmarkningarAnkomst": "SJ Regional",
"AnnonseradAnkomstTid": "21:50",
"AnnonseradAvgangTid": "21:53",
"BeraknadAvgangTid": null,
"BeraknadAnkomstTid": null,
"AnnonseradTidpunktAvgang": "2011-10-23T21:53:00+02:00"
}, {
"__type": "Trafikverket_API.Models.Trafiklage",
"TagGrupp": "eae407a9-61ba-43f4-9c53-c666c2a3f117",
"Fran": "Stockholm,Södertälje",
"Till": "Norrköping,Linköping",
"TrafikplatsNamn": "Nyköping C",
"VisaAnkomst": "true",
"InstalldAnkomst": "false",
"InstalldAvgang": "false",
"SparangivelseAvgang": "6",
"AnmarkningarAvgang": "SJ Regional",
"AnnonseratTagId": "251",
"SparangivelseAnkomst": "6",
"AnmarkningarAnkomst": "SJ Regional",
"AnnonseradAnkomstTid": "22:52",
"AnnonseradAvgangTid": "22:54",
"BeraknadAvgangTid": null,
"BeraknadAnkomstTid": null,
"AnnonseradTidpunktAvgang": "2011-10-23T22:54:00+02:00"
}, ..... ]
}
我有一個叫StationInfo對象,看起來像這樣:
public class DataModel
{
public Boolean VisaAnkomst;
public String TagGrupp;
public String Fran;
public String Till;
public String TrafikplatsNamn;
public Boolean InstalldAnkomst;
public Boolean InstalldAvgang;
public String SparangivelseAvgang;
public String AnmarkningarAvgang;
public Integer AnnonseratTagId;
public String SparangivelseAnkomst;
public String AnmarkningarAnkomst;
public String AnnonseradAnkomstTid;
public String AnnonseradAvgangTid;
public String BeraknadTidpunktAnkomst;
public String BeraknadTidpunktAvgang;
public String VerkligAnkomstTid;
public String VerkligAvgangTid;
public String BeraknadAvgangTid;
public String BeraknadAnkomstTid;
public String AnnonseradTidpunktAvgang;
public String VerkligTidpunktAnkomst;
}
當我運行此:
Gson gson = new Gson();
DataModel[] stationInfos = gson.fromJson(result, DataModel[].class);
我得到這個異常:
com.google.gson.JsonParseException: Expecting object but found array
我ges我需要一些匹配對象「d」的對象,但我該怎麼做?
嘗試添加新的類'類{DotNetDataModel公衆的DataModel [] d; }'並像這樣使用'DotNetDataModel [] resp = gson.fromJson(result,DotNetDataModel.class); DataModel [] stationInfos = resp.d;' – Selvin