为什么Gson fromJson抛出JsonSyntaxException:期望某种类型,但是是其他类型? [英] Why does Gson fromJson throw a JsonSyntaxException: Expected some type but was some other type?
问题描述
(这篇文章的目的是成为一个 我试图反序列化一些使用 这段代码 抛出以下异常 为什么可以't Gson正确地将我的JSON文本转换为我的POJO类型? b 反序列化时,Gson期待JSON对象,但找到了一个JSON数组。由于它不能从一个转换到另一个,它抛出了这个异常。 JSON格式描述此处。简而言之,它定义了以下类型:对象,数组,字符串,数字, 在Gson(和大多数JSON解析器)中,存在以下映射:JSON字符串映射到Java Gson遍历您提供的JSON内容并尝试将其反序列化为您请求的对应类型。如果内容不匹配或无法转换为预期类型,则会抛出相应的异常。 在您的情况中,您提供了以下JSON 在根目录下,这是一个JSON对象,它包含一个名为 然而,您已将 既不是数组类型也不是 相反,您有3个选项: 更改您的JSON以匹配预期类型 li> 将您的 为 (This post is meant to be a canonical question with a sample answer provided below.) I'm trying to deserialize some JSON content into a custom POJO type with This piece of code throws the follow exception Why can't Gson properly convert my JSON text to my POJO type? As the exception message states while deserializing, Gson was expecting a JSON object, but found a JSON array. Since it couldn't convert from one to the other, it threw this exception. The JSON format is described here. In short, it defines the following types: objects, arrays, strings, numbers, In Gson (and most JSON parsers), the following mappings exist: a JSON string maps to a Java Gson iterates through the JSON content that you provide and tries to deserialize it to the corresponding type you've requested. If the content doesn't match or can't be converted to the expected type, it'll throw a corresponding exception. In your case, you provided the following JSON At the root, this is a JSON object which contains a member named However, you've defined your that is neither an array type, nor a Instead, you have 3 options: Change your JSON to match the expected type Change your Write and register a custom deserializer for 这篇关于为什么Gson fromJson抛出JsonSyntaxException:期望某种类型,但是是其他类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Gson#fromJson(String,Class)
。
import com.google.gson.Gson;
$ b public class Sample {
public static void main(String [] args){
String json ={\nestedPojo \:[{\name \:null,\value \:42}]};
Gson gson = new Gson();
gson.fromJson(json,Pojo.class);
}
}
class Pojo {
NestedPojo nestedPojo;
}
class NestedPojo {
字符串名称;
int值;
}
线程main中的异常com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但是BEGIN_ARRAY在第1行第16列路径$ .nestedPojo
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.Gson.fromJson(Gson.java:810)
在com.google.gson.Gson.fromJson(Gson.java:775)
在com.google.gson.Gson.fromJson(Gson.java:724)
在com.google.gson。 Gson.fromJson(Gson.java:696)
at com.example.Sample.main(Sample.java:23)
引起:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但是BEGIN _ARRAY在第1行第16列路径$ .nestedPojo
在com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:189)
... 7 more
导致:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第1行第16列的BEGIN_ARRAY $ .nestedPojo
null
和布尔值 true
和 false
。
String
;一个JSON数字映射到Java Number
类型; JSON数组映射到集合
类型或数组类型; JSON对象映射到Java Map
类型,或者通常是自定义的 POJO 类型(以前未提及); null
映射到Java的 null
,并且布尔值映射到Java的 true
和 false
。
{
nestedPojo:[
{
name:null,
值:42
}
]
}
nestedPojo
的成员,它是一个JSON数组。该JSON数组包含单个元素,另一个具有两个成员的JSON对象。考虑到前面定义的映射,你可以期望这个JSON映射到一个Java对象,它有一个名为 nestedPojo
的 name
和 value
的字段。
Pojo
类型定义为有一个字段
NestedPojo nestedPojo;
集合
类型。 Gson无法反序列化此字段的相应JSON。
{
nestedPojo: {
name:null,
value:42
}
}
Pojo
类型更改为期望集合
或数组类型
列表< NestedPojo> nestedPojo; //考虑更改名称并使用@SerializedName
NestedPojo [] nestedPojo;
NestedPojo写入并注册一个自定义的反序列化器
与您自己的解析规则。例如
类定制实现JsonDeserializer< NestedPojo> {
@Override
public NestedPojo deserialize(JsonElement json,Type typeOfT,JsonDeserializationContext context)throws JsonParseException {
NestedPojo nestedPojo = new NestedPojo();
JsonArray jsonArray = json.getAsJsonArray();
if(jsonArray.size()!= 1){
throw new IllegalStateException(unexpected json);
}
JsonObject jsonObject = jsonArray.get(0).getAsJsonObject(); //只获得元素
JsonElement jsonElement = jsonObject.get(name);
if(!jsonElement.isJsonNull()){
nestedPojo.name = jsonElement.getAsString();
}
nestedPojo.value = jsonObject.get(value)。getAsInt();
返回nestedPojo;
$ b $ gson gson = new GsonBuilder()。registerTypeAdapter(NestedPojo.class,new Custom())。create();
Gson#fromJson(String, Class)
.import com.google.gson.Gson;
public class Sample {
public static void main(String[] args) {
String json = "{\"nestedPojo\":[{\"name\":null, \"value\":42}]}";
Gson gson = new Gson();
gson.fromJson(json, Pojo.class);
}
}
class Pojo {
NestedPojo nestedPojo;
}
class NestedPojo {
String name;
int value;
}
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 16 path $.nestedPojo
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at com.google.gson.Gson.fromJson(Gson.java:724)
at com.google.gson.Gson.fromJson(Gson.java:696)
at com.example.Sample.main(Sample.java:23)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 16 path $.nestedPojo
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
... 7 more
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 16 path $.nestedPojo
null
, and the boolean values true
and false
.String
; a JSON number maps to a Java Number
type; a JSON array maps to a Collection
type or an array type; a JSON object maps to a Java Map
type or, typically, a custom POJO type (not mentioned previously); null
maps to Java's null
, and the boolean values map to Java's true
and false
.{
"nestedPojo": [
{
"name": null,
"value": 42
}
]
}
nestedPojo
which is a JSON array. That JSON array contains a single element, another JSON object with two members. Considering the mappings defined earlier, you'd expect this JSON to map to a Java object which has a field named nestedPojo
of some Collection
or array type, where that types defines two fields named name
and value
, respectively.Pojo
type as having a fieldNestedPojo nestedPojo;
Collection
type. Gson can't deserialize the corresponding JSON for this field.
{
"nestedPojo": {
"name": null,
"value": 42
}
}
Pojo
type to expect a Collection
or array typeList<NestedPojo> nestedPojo; // consider changing the name and using @SerializedName
NestedPojo[] nestedPojo;
NestedPojo
with your own parsing rules. For exampleclass Custom implements JsonDeserializer<NestedPojo> {
@Override
public NestedPojo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
NestedPojo nestedPojo = new NestedPojo();
JsonArray jsonArray = json.getAsJsonArray();
if (jsonArray.size() != 1) {
throw new IllegalStateException("unexpected json");
}
JsonObject jsonObject = jsonArray.get(0).getAsJsonObject(); // get only element
JsonElement jsonElement = jsonObject.get("name");
if (!jsonElement.isJsonNull()) {
nestedPojo.name = jsonElement.getAsString();
}
nestedPojo.value = jsonObject.get("value").getAsInt();
return nestedPojo;
}
}
Gson gson = new GsonBuilder().registerTypeAdapter(NestedPojo.class, new Custom()).create();