用gson和空值反序列化 [英] Deserialize with gson and null values

查看:537
本文介绍了用gson和空值反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用空值反序列化我自己的类。但我的代码无法正常工作。



我的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:使用JsonReader.setLenient(true)在第1行接受格式错误的JSON第6列路径$ 
myapp W / System.err?在com.google.gson.Gson.assertFullConsumption(Gson.java:786)
myapp W / System.err?在com.google.gson.Gson.fromJson(Gson.java:776)
myapp W / System.err?在com.google.gson.Gson.fromJson(Gson.java:724)
myapp W / System.err? at com.google.gson.Gson.fromJson(Gson.java:696)


解决方案

首先,你必须阅读如何使用gson进行解析。您可以在此处找到一些示例。



现在你知道如何解析,你仍然可以有空值的问题。为了解决它,你必须告诉gson使用

序列化 null

  Gson gson = new GsonBuilder()。serializeNulls()。create(); 

serializeNulls() doc
$ b


配置Gson以序列化空字段。默认情况下,Gson在序列化过程中省略所有空字段。


编辑 doc)

为了得到一些不同的值,你可以做

pre $ code > String json =; //你的json有一个字符串
JsonObject jsonObject = new JsonParser()。parse(json).getAsJsonObject();

//如果为null,则使用默认值
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)
私有字符串标题;

@SerializedName(Code)
private int code;
}

您可以使用

  String json =; //你的json有一个字符串
Gson gson = new GsonBuilder()。serializeNulls()。create();
MyElement myElement = gson.fromJson(json,MyElement.class);


I am trying to deserialize my own class with a null value. But my code doesn't work.

My json:

{"Text":null,"Code":0,"Title":"This is Sparta!"}

In my method I do the following:

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))

I am not deserialize the whole object, because there can be a List<T>, too.

The error:

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)

解决方案

First, you must read about how to parse using gson. You can find some example here.

Now you know how to parse, you can still have problem with null values. To solve it you must tell gson to (de)serialize null using

Gson gson = new GsonBuilder().serializeNulls().create();

From the serializeNulls() doc

Configure Gson to serialize null fields. By default, Gson omits all fields that are null during serialization.

EDIT (Not tested, based on doc)

In order to get some distinct value you can do

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();

Otherwise if you have this pojo

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

    @SerializedName("Title")
    private String title;

    @SerializedName("Code")
    private int code;
}

you can parse using

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

这篇关于用gson和空值反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆