使用 gson 和 null 值反序列化
Deserialize with gson and null values
我正在尝试使用空值反序列化我自己的 class。但是我的代码不起作用。
我的json:
{"Text":null,"Code":0,"Title":"This is Sparta!"}
在我的方法中,我执行以下操作:
this.setText(gson.fromJson(jsonObject.getString("Text"), String.class));
this.setTitle(gson.fromJson(jsonObject.getString("Title"), String.class));
this.setCode(gson.fromJson(jsonObject.getString("Faccode"), Integer.class))
我不会反序列化整个对象,因为也可以有一个List<T>
。
错误:
myapp W/System.err? com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 6 path $
myapp W/System.err? at com.google.gson.Gson.assertFullConsumption(Gson.java:786)
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:776)
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:724)
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:696)
首先,您必须了解如何使用 gson 进行解析。你可以找到一些例子 here.
现在您知道如何解析了,但您仍然会遇到空值问题。要解决它,您必须告诉 gson 使用
(反)序列化 null
Gson gson = new GsonBuilder().serializeNulls().create();
来自serializeNulls()
doc
Configure Gson to serialize null fields. By default, Gson omits all fields that are null during serialization.
编辑(未测试,基于文档)
为了获得一些独特的价值,你可以这样做
String json = ""; //Your json has a String
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
//If null, use a default value
JsonElement nullableText = jsonObject.get("Text");
String text = (nullableText instanceof JsonNull) ? "" : nullableText.getAsString();
String title = jsonObject.get("Title").toString();
int code = jsonObject.get("Code").getAsInt();
否则如果你有这个pojo
public class MyElement {
@SerializedName("Text")
private String text;
@SerializedName("Title")
private String title;
@SerializedName("Code")
private int code;
}
您可以使用
进行解析
String json = ""; //Your json has a String
Gson gson = new GsonBuilder().serializeNulls().create();
MyElement myElement = gson.fromJson(json, MyElement.class);
我在以下 POJO 中遇到了类似的问题(在 null
值上抛出异常):
public class MyElement {
private String something;
private String somethingElse;
private JsonObject subEntry; // this doesn't allow deserialization of `null`!
}
和此代码:
parsedJson = gson.fromJson(json, MyElement.class)
当后端返回 subEntry
时 null
。
我通过将 subEntry
的类型从 JsonObject
更改为 JsonElement
来修复它,它是 JsonObject
和 [=21] 的父 class =],以允许反序列化 null
个值。
public class MyElement {
private String something;
private String somethingElse;
private JsonElement subEntry; // this allows deserialization of `null`
}
稍后要在运行时检查 null,您需要执行以下操作:
if (parsedJson.subEntry instanceof JsonNull) {
...
} else {
...
}
我正在尝试使用空值反序列化我自己的 class。但是我的代码不起作用。
我的json:
{"Text":null,"Code":0,"Title":"This is Sparta!"}
在我的方法中,我执行以下操作:
this.setText(gson.fromJson(jsonObject.getString("Text"), String.class));
this.setTitle(gson.fromJson(jsonObject.getString("Title"), String.class));
this.setCode(gson.fromJson(jsonObject.getString("Faccode"), Integer.class))
我不会反序列化整个对象,因为也可以有一个List<T>
。
错误:
myapp W/System.err? com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 6 path $
myapp W/System.err? at com.google.gson.Gson.assertFullConsumption(Gson.java:786)
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:776)
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:724)
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:696)
首先,您必须了解如何使用 gson 进行解析。你可以找到一些例子 here.
现在您知道如何解析了,但您仍然会遇到空值问题。要解决它,您必须告诉 gson 使用
(反)序列化null
Gson gson = new GsonBuilder().serializeNulls().create();
来自serializeNulls()
doc
Configure Gson to serialize null fields. By default, Gson omits all fields that are null during serialization.
编辑(未测试,基于文档)
为了获得一些独特的价值,你可以这样做
String json = ""; //Your json has a String
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
//If null, use a default value
JsonElement nullableText = jsonObject.get("Text");
String text = (nullableText instanceof JsonNull) ? "" : nullableText.getAsString();
String title = jsonObject.get("Title").toString();
int code = jsonObject.get("Code").getAsInt();
否则如果你有这个pojo
public class MyElement {
@SerializedName("Text")
private String text;
@SerializedName("Title")
private String title;
@SerializedName("Code")
private int code;
}
您可以使用
进行解析String json = ""; //Your json has a String
Gson gson = new GsonBuilder().serializeNulls().create();
MyElement myElement = gson.fromJson(json, MyElement.class);
我在以下 POJO 中遇到了类似的问题(在 null
值上抛出异常):
public class MyElement {
private String something;
private String somethingElse;
private JsonObject subEntry; // this doesn't allow deserialization of `null`!
}
和此代码:
parsedJson = gson.fromJson(json, MyElement.class)
当后端返回 subEntry
时 null
。
我通过将 subEntry
的类型从 JsonObject
更改为 JsonElement
来修复它,它是 JsonObject
和 [=21] 的父 class =],以允许反序列化 null
个值。
public class MyElement {
private String something;
private String somethingElse;
private JsonElement subEntry; // this allows deserialization of `null`
}
稍后要在运行时检查 null,您需要执行以下操作:
if (parsedJson.subEntry instanceof JsonNull) {
...
} else {
...
}