简体   繁体   English

Json Gson期望Begin Object但是字符串

[英]Json Gson expected Begin Object but was string

I am using the following code to contain this Json 我正在使用以下代码包含此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;
    }
}

and the json: 和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"
     },

I am building my Gson like so: 我正在像这样构建我的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;
    }
}

the reason for this is because the list of "Note.class" can comeback as a list or as a single object. 其原因是因为“ Note.class”的列表可以作为列表或单个对象返回。

And finally building it like this 最后像这样构建它

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

Something is going worng ehre and I cant figure out what. 事情正在发生,我不知道该怎么办。 It has been troubling me for the past few hours. 在过去的几个小时里,这一直困扰着我。

I get this error 我得到这个错误

    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

Does anyone have any idea what is going wrong? 有谁知道出什么问题了吗?

the line 1932 in my json points here 我的json中的1932行指向此处

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

If I'm not mistaken there's some JSON parse error as mentioned in the error, at: 如果我没记错的话,那么错误中会提到一些JSON解析错误:

line 1932 column 20

What does your JSON look like at that line? 您的JSON在那行是什么样的?

Your RequestDTO contains the field notes that is an Object. 您的RequestDTO包含作为对象的字段注释。

@SerializedName ("notes")
public Notes notes;

But the JSON gives you a string 但是JSON给你一个字符串

"notes": "", 

Hence the error. 因此,错误。

The solution would be to change Notes to String. 解决方案是将Notes更改为String。

@SerializedName ("notes")
public String notes;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM