即时开发Android应用程序,应解析公共交通的基本时刻表。基本部分工作得很好,但我遇到了JSON列表的问题。
JSON respone看起来像这样
{"Departures": [{
"Id": "68496628",
"LineName": "15",
"Direction": "Munic",
"Platform": {
"Name": "4",
"Type": "Platform"
},
"Mot": "Tram",
"RealTime": "\/Date(1501101000000+0200)\/",
"ScheduledTime": "\/Date(1501101000000+0200)\/",
"State": "InTime",
"RouteChanges": [
"510322",
"510447",
"510448"
],
"Diva": {
"Number": "11015",
"Network": "ooe"
}]}
我的问题是解析Liste RouteChanges。有时在Response中列表为空,有时候是这样的列表。 我在Android中获取列表的代码如下所示:
public static class Cast {
private String name="0";
public String getName() {
Log.d("Model","String from first List-Entry is: "+name);
return name;
}
}
但是在每种情况下,当我得到一条带有RouteChanges-list的行时,我的应用程序崩溃了以下的Log-Mon-Message:
07-26 22:09:00.628 9578-17541/com.hisham.jsonparsingdemo E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.hisham.jsonparsingdemo, PID: 9578
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 237 path $.RouteChanges[0]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at com.google.gson.Gson.fromJson(Gson.java:724)
at com.google.gson.Gson.fromJson(Gson.java:696)
at com.hisham.jsonparsingdemo.MainActivity$JSONTask.doInBackground(MainActivity.java:149)
at com.hisham.jsonparsingdemo.MainActivity$JSONTask.doInBackground(MainActivity.java:85)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 237 path $.RouteChanges[0]
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at com.google.gson.Gson.fromJson(Gson.java:724)
at com.google.gson.Gson.fromJson(Gson.java:696)
at com.hisham.jsonparsingdemo.MainActivity$JSONTask.doInBackground(MainActivity.java:149)
at com.hisham.jsonparsingdemo.MainActivity$JSONTask.doInBackground(MainActivity.java:85)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
我的MainActivity中的第149行是Gson-Line:
Model movieModel = gson.fromJson(finalObject.toString(), MovieModel.class); // a single line json parsing using Gson
finalobject是通过以下方式创建的:
for(int i=0; i<parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
}
通过JSON数组的parentArray,它将关键字的字符串拆分为&#34; Departures&#34;
你能帮助我通过该列表阅读gson吗?
谢谢
编辑:整个MovieModelClass:
public class MovieModel {
@SerializedName("RouteChanges")
private String LineName;
private String Direction;
private String State;
private String ScheduledTime;
private String RealTime;
private String HstOrt;
private String HstName;
private String HstId;
public static class Cast {
private String name="0";
public String getName() {
return name;
}
}
public String getLineName(){return LineName;}
public String getDirection(){return Direction;}
public String getState(){return State;}
public String getScheduledTime(){return ScheduledTime;}
public String getRealTime(){return RealTime;}
public String getHstOrt(){return HstOrt;}
public String getHstName(){return HstName;}
public String getHstId(){return HstId;}}
答案 0 :(得分:1)
你需要放一个大括号&#34; {&#34;在开始时:
{
"Departures": [{
"Id": "68496628",
"LineName": "15",
"Direction": "Munic",
"Platform": {
"Name": "4",
"Type": "Platform"
},
"Mot": "Tram",
"RealTime": "\/Date(1501101000000+0200)\/",
"ScheduledTime": "\/Date(1501101000000+0200)\/",
"State": "InTime",
"RouteChanges": [
"510322",
"510447",
"510448"
],
"Diva": {
"Number": "11015",
"Network": "ooe"
}]
}
这条消息让我了解了你的错误:
预计BEGIN_OBJECT但第1行为STRING
更改此行:
String LineName
为此:
List<String> LineName
答案 1 :(得分:0)
{
"Departures":[
{
"Id":"68496628",
"LineName":"15",
"Direction":"Munic",
"Platform":{
"Name":"4",
"Type":"Platform"
},
"Mot":"Tram",
"RealTime":"\/Date(1501101000000+0200)\/",
"ScheduledTime":"\/Date(1501101000000+0200)\/",
"State":"InTime",
"RouteChanges":[
"510322",
"510447",
"510448"
],
"Diva":{
"Number":"11015",
"Network":"ooe"
}
}
]}
还缺少一个右大括号} 。您始终可以在Postman或在线JSONformatter
中测试您的JSON