2012-11-08 15 views
0

我使用下面的代碼包含此JSON的Json GSON預期開始對象卻被串

public class RequestsDTO 
{ 
    @SerializedName ("requests") 
    public Requests requestsContainer; 

    public class Requests 
    { 
     @SerializedName ("request") 
     public List<Request> requests;  
    } 

    public class Request 
    { 

     @SerializedName ("amount") 
     public Amount amount; 

     @SerializedName ("id") 
     public int requestId; 

     @SerializedName ("status") 
     public Status status; 

     @SerializedName ("created") 
     public String created; 
     @SerializedName ("start") 
     public String start; 

     @SerializedName ("notes") 
     public Notes notes; 

     @SerializedName ("type") 
     public Type type; 

     @SerializedName ("employee") 
     public Employee employee; 

     @SerializedName ("end") 
     public String end; 
    } 

    public class Amount 
    { 

     @SerializedName ("content") 
     public int time; 
     @SerializedName ("unit") 
     public String unit; 
    } 
    public class Status 
    { 

     @SerializedName ("content") 
     public String currentStatus; 
     @SerializedName ("lastChanged") 
     public String lastChangedDate; 
     @SerializedName ("lastChangedByUserId") 
     public int lastChangedByUserId; 
    } 
    public class Notes 
    { 
     @SerializedName ("note") 
     public List<Note> note; 
    } 
    public class Note 
    { 
     @SerializedName ("content") 
     public String content; 
     @SerializedName ("from") 
     public String from; 
    } 
    public class Type 
    { 
     @SerializedName ("content") 
     public String content; 
     @SerializedName ("id") 
     public int id; 
    } 
    public class Employee 
    { 
     @SerializedName ("content") 
     public String content; 
     @SerializedName ("id") 
     public int id; 
    } 
} 

和JSON:

{"requests": {"request": [ 
    { 
     "amount": { 
      "content": 2, 
      "unit": "hours" 
     }, 
     "id": 246, 
     "status": { 
      "content": "superceded", 
      "lastChanged": "2012-10-24", 
      "lastChangedByUserId": 2270 
     }, 
     "created": "2012-10-11", 
     "start": "2012-10-20", 
     "notes": {"note": [ 
      { 
       "content": "Having wisdom teeth removed.", 
       "from": "employee" 
      }, 
      { 
       "content": "Get well soon", 
       "from": "manager" 
      } 
     ]}, 
     "type": { 
      "content": "Vacation", 
      "id": 4 
     }, 
     "employee": { 
      "content": "Michael Daniels", 
      "id": 40350 
     }, 
     "end": "2012-10-25" 
    }, 

我建設我的GSON像這樣:

public class Json { 
    private static Gson gson; 

    private static class MyNoteClassTypeAdapter implements JsonDeserializer<List<RequestsDTO.Note>> { 
     public List<RequestsDTO.Note> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) { 
      List<RequestsDTO.Note> vals = new ArrayList<RequestsDTO.Note>(); 
      if (json.isJsonArray()) { 
       for (JsonElement e : json.getAsJsonArray()) { 
        vals.add((RequestsDTO.Note) ctx.deserialize(e, RequestsDTO.Note.class)); 
       } 
      } else if (json.isJsonObject()) { 
       vals.add((RequestsDTO.Note) ctx.deserialize(json,RequestsDTO.Note.class)); 
      } else { 
       throw new RuntimeException("Unexpected JSON type: " + json.getClass()); 
      } 
      return vals; 
     } 
    } 

    public static Gson getGson() 
    { 
     if (gson == null) 
     { 
      Type ListType = new TypeToken<List<RequestsDTO.Note>>() {}.getType(); 
      GsonBuilder builder = new GsonBuilder(); 
      builder.registerTypeAdapter(DateTime.class, new DateTimeSerializer()); 
      builder.registerTypeAdapter(ListType, new MyNoteClassTypeAdapter()); 
      gson = builder.create(); 
     } 
     return gson; 
    } 
} 

這樣做的原因是,因爲「Note.class」即可復出爲列表或作爲單個對象的列表。

最後建造像這樣

Json.getGson().fromJson(response, RequestsDTO.class); 

有些事情是會撥錯EHRE我無法弄清楚什麼。過去幾個小時一直困擾着我。

我得到這個錯誤

11-07 20:04:57.097: E/AndroidRuntime(6963): FATAL EXCEPTION: main 
11-07 20:04:57.097: E/AndroidRuntime(6963): java.lang.RuntimeException: Unable to start activity 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1648) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1662) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.os.Looper.loop(Looper.java:130) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.app.ActivityThread.main(ActivityThread.java:3696) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at java.lang.reflect.Method.invoke(Method.java:507) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at dalvik.system.NativeStart.main(Native Method) 
11-07 20:04:57.097: E/AndroidRuntime(6963): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1932 column 20 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.Gson.fromJson(Gson.java:795) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.Gson.fromJson(Gson.java:761) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.Gson.fromJson(Gson.java:710) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.Gson.fromJson(Gson.java:682) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.mokinetworks.bamboohr.service.WebService.getTimeOffRequests(WebService.java:154) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.mokinetworks.bamboohr.ViewRequestActivity.onCreate(ViewRequestActivity.java:29) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  ... 11 more 
11-07 20:04:57.097: E/AndroidRuntime(6963): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1932 column 20 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.stream.JsonReader.expect(JsonReader.java:339) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165) 
11-07 20:04:57.097: E/AndroidRuntime(6963):  ... 28 more 

沒有人有任何想法是怎麼回事?

行1932年在這裏

"created": "2012-10-20", 
    "start": "2012-10-29", 
    "notes": "",   <---------line 1932 
    "type": { 
     "content": "Vacation", 
     "id": 4 
    }, 
+0

什麼是完整的堆棧跟蹤? –

+0

我現在已經包含完整的堆棧跟蹤 – WIllJBD

+0

您可以發佈完整的JSON請。 –

回答

1

的json點。如果我沒有記錯的有一些JSON解析錯誤中提到的錯誤,在:

line 1932 column 20 

什麼是您的JSON的樣子像那條線?

+0

好的,這是問題所在,顯然筆記是三件事情之一,筆記,筆記的單個對象或「註釋」中的一個:「」, – WIllJBD

0

你RequestDTO包含字段指出,是一個對象。

@SerializedName ("notes") 
public Notes notes; 

但JSON給你一個字符串

"notes": "", 

因此錯誤。

解決辦法是改變註釋字符串。

@SerializedName ("notes") 
public String notes; 
+0

很久以前,通過編寫自定義序列化程序解決了這個問題。然而,更簡單的解決方案應該是讓服務團隊重新思考他們發送數據的不正確方式。不幸的是,我們無法得到他們,因此解決方案。真正的問題源於其糟糕的體系結構,其中「notes」在沒有對象(空數據)時爲空字符串,當具有單個實體時爲JSONObject,在具有多於1個實體時爲JSONArray。該服務應始終返回一個數組,無論它是否爲空,包含單個對象或多個對象。 – WIllJBD