使用gson和null值反序列化

BHu*_*lse 12 android gson

我试图用null值反序列化我自己的类.但我的代码不起作用.

我的json:

{"Text":null,"Code":0,"Title":"This is Sparta!"}
Run Code Online (Sandbox Code Playgroud)

在我的方法中,我执行以下操作:

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))
Run Code Online (Sandbox Code Playgroud)

我没有反序列化整个对象,因为也可以有一个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)
Run Code Online (Sandbox Code Playgroud)

Tho*_*aud 28

首先,您必须阅读有关如何使用gson进行解析的信息.你可以在这里找到一些例子.

现在你知道如何解析,你仍然可以解决空值问题.为了解决这个问题,你必须告诉GSON到(反)序列null使用

Gson gson = new GsonBuilder().serializeNulls().create();
Run Code Online (Sandbox Code Playgroud)

来自serializeNulls() doc

配置Gson以序列化空字段.默认情况下,Gson省略序列化期间为空的所有字段.

编辑(未经测试,基于doc)

为了获得一些独特的价值,你可以做到

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();
Run Code Online (Sandbox Code Playgroud)

否则,如果你有这个pojo

public class MyElement {
    @SerializedName("Text")
    private String text;

    @SerializedName("Title")
    private String title;

    @SerializedName("Code")
    private int code;
}
Run Code Online (Sandbox Code Playgroud)

你可以解析使用

String json = ""; //Your json has a String
Gson gson = new GsonBuilder().serializeNulls().create();
MyElement myElement = gson.fromJson(json, MyElement.class);
Run Code Online (Sandbox Code Playgroud)

  • `.serializeNulls()`部分是无关紧要的,它只与序列化(对象 - > json)有关,反之亦然. (13认同)
  • 我已经读了很多。我也尝试了Gson gson = new GsonBuilder()。serializeNulls()。create();。但是没有得到。我必须如何正确解析?问题不在于序列化,而在于反序列化 (3认同)

jak*_*b.g 5

null在以下 POJO 中遇到了类似的问题(值抛出异常):

public class MyElement {
    private String something;
    private String somethingElse;
    private JsonObject subEntry; // this doesn't allow deserialization of `null`!
}
Run Code Online (Sandbox Code Playgroud)

和这个代码:

parsedJson = gson.fromJson(json, MyElement.class)
Run Code Online (Sandbox Code Playgroud)

subEntry后端返回时是null.

我通过改变类型固定它subEntryJsonObjectJsonElement该父类二者的JsonObjectJsonNull,以允许反序列化null值。

public class MyElement {
    private String something;
    private String somethingElse;
    private JsonElement subEntry; // this allows deserialization of `null`
}
Run Code Online (Sandbox Code Playgroud)

要稍后在运行时检查 null,您可以执行以下操作:

if (parsedJson.subEntry instanceof JsonNull) {
    ...
} else {
    ...
}
Run Code Online (Sandbox Code Playgroud)