Json, Gson erwartet Beginnen Objekt wurde aber string

Ich bin mit dem folgenden code zu enthalten, die das Json -

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;
    }
}

und 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"
     },

Bin ich mit dem Aufbau meiner Gson so:

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;
    }
}

der Grund für dieses ist, weil die Liste "Note.class" kann comeback als eine Liste oder als ein einzelnes Objekt.

Und schließlich Bau es so

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

Etwas Los ist worng ehre und ich kann nicht herausfinden, was. Es wurde beunruhigend mich für die letzten paar Stunden.

Bekomme ich diesen Fehler

    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

Hat jemand eine Idee was schief läuft?

die Linie 1932 in meine json-Punkte hier

 "created": "2012-10-20",
    "start": "2012-10-29",
    "notes": "",            <---------line 1932
    "type": {
        "content": "Vacation",
        "id": 4
    },
  • Was ist der volle stack-trace?
  • Ich habe jetzt enthalten den vollständigen stack-trace
  • Kannst du die komplette JSON-bitte.
  • Die komplette Json-2400 Linien
  • es ist die schlechte Leitung
InformationsquelleAutor WIllJBD | 2012-11-08
Schreibe einen Kommentar