public Person jsonToObject(){
String str = "[{\"name\":\"name0\",\"age\":0}]";
Person person = gson.fromJson(str, Person.class);
return person;
}
解析报异常如下:
Exception in thread "main" com.google.gson.JsonParseException: Expecting array but found object: null:0
at com.google.gson.JsonArrayDeserializationVisitor.startVisitingObject(JsonArrayDeserializationVisitor.java:89)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:119)
at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:67)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:52)
at com.google.gson.Gson.fromJson(Gson.java:551)
at com.google.gson.Gson.fromJson(Gson.java:498)
at com.google.gson.Gson.fromJson(Gson.java:467)
at com.google.gson.Gson.fromJson(Gson.java:417)
at com.google.gson.Gson.fromJson(Gson.java:389)
at test.TestGson01.jsonToObject(TestGson01.java:14)
at test.TestGson01.main(TestGson01.java:47)
错误代码:
String str = "[{\"name\":\"name0\",\"age\":0}]";
应该写为:
String str = "{\"name\":\"name0\",\"age\":0}";