我正在使用以下代码来包含此 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;
}
}
和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);
有些东西正在磨损,我不知道是什么。在过去的几个小时里,它一直困扰着我。
我收到这个错误
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
有谁知道出了什么问题?
我的 json 中的第 1932 行指向这里
"created": "2012-10-20",
"start": "2012-10-29",
"notes": "", <---------line 1932
"type": {
"content": "Vacation",
"id": 4
},