gson中null值替换,gson自动过滤值为null的属性

本文介绍使用Gson解析JSON时遇到服务器返回null值的问题及解决方案。通过自定义适配器将null转换为空字符串,确保数据的一致性和可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

json解析有很多工具,这里说的是最常用也是解析速度最快的Gson,Gson是google家出的,有一个缺点就是无法设置null替换,
我们只能手动的批量替换服务器返回的null了,正常的接口定义的时候是绝对不允许服务器返回null的,后台结果却总会出现null!
如果搜索的话有一个常见的答案,
Gson gson = new GsonBuilder().serializeNulls().create();
但是这个却无法解决反序列问题,怎么解决呢?我在stackoverflow上找到了这个问题,亲测有效
http://stackoverflow.com/questions/9483348/gson-treat-null-as-empty-string/24252578#24252578

解决办法如下:

<code class="scala" style="padding: 0px; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; border: none; background-color: transparent;"><span class="hljs-type" style="color: rgb(181, 137, 0);">Gson</span> gson  = <span class="hljs-keyword" style="color: rgb(133, 153, 0);">new</span> <span class="hljs-type" style="color: rgb(181, 137, 0);">GsonBuilder</span>().registerTypeAdapterFactory(<span class="hljs-keyword" style="color: rgb(133, 153, 0);">new</span> <span class="hljs-type" style="color: rgb(181, 137, 0);">NullStringToEmptyAdapterFactory</span>()).create();
<span class="hljs-comment" style="color: rgb(147, 161, 161);">//然后用上面一行写的gson来序列化和反序列化实体类type</span>
gson.fromJson(json, <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">type</span>);</span>
gson.toJson(<span class="hljs-class"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">type</span>);</span></code>

//NullStringToEmptyAdapterFactory的代码

<code class="scala" style="padding: 0px; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; border: none; background-color: transparent;">public <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">class</span> <span class="hljs-title" style="color: rgb(181, 137, 0);">NullStringToEmptyAdapterFactory<T></span> <span class="hljs-title" style="color: rgb(181, 137, 0);">implements</span> <span class="hljs-title" style="color: rgb(181, 137, 0);">TypeAdapterFactory</span> {</span>
    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@SuppressWarnings</span>(<span class="hljs-string" style="color: rgb(42, 161, 152);">"unchecked"</span>)
    public <<span class="hljs-type" style="color: rgb(181, 137, 0);">T</span>> <span class="hljs-type" style="color: rgb(181, 137, 0);">TypeAdapter</span><<span class="hljs-type" style="color: rgb(181, 137, 0);">T</span>> create(<span class="hljs-type" style="color: rgb(181, 137, 0);">Gson</span> gson, <span class="hljs-type" style="color: rgb(181, 137, 0);">TypeToken</span><<span class="hljs-type" style="color: rgb(181, 137, 0);">T</span>> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">type</span>) {</span>
        <span class="hljs-type" style="color: rgb(181, 137, 0);">Class</span><<span class="hljs-type" style="color: rgb(181, 137, 0);">T</span>> rawType = (<span class="hljs-type" style="color: rgb(181, 137, 0);">Class</span><<span class="hljs-type" style="color: rgb(181, 137, 0);">T</span>>) <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">type</span>.<span class="hljs-title" style="color: rgb(181, 137, 0);">getRawType</span>(</span>);
        <span class="hljs-keyword" style="color: rgb(133, 153, 0);">if</span> (rawType != <span class="hljs-type" style="color: rgb(181, 137, 0);">String</span>.<span class="hljs-keyword" style="color: rgb(133, 153, 0);">class</span>) {
            <span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span>;
        }
        <span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span> (<span class="hljs-type" style="color: rgb(181, 137, 0);">TypeAdapter</span><<span class="hljs-type" style="color: rgb(181, 137, 0);">T</span>>) <span class="hljs-keyword" style="color: rgb(133, 153, 0);">new</span> <span class="hljs-type" style="color: rgb(181, 137, 0);">StringNullAdapter</span>();
    }
}</code>

// StringNullAdapter代码

<code class="scala" style="padding: 0px; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; border: none; background-color: transparent;">public <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">class</span> <span class="hljs-title" style="color: rgb(181, 137, 0);">StringNullAdapter</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span></span> <span class="hljs-title" style="color: rgb(181, 137, 0);">TypeAdapter<String></span> {</span>
    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    public <span class="hljs-type" style="color: rgb(181, 137, 0);">String</span> read(<span class="hljs-type" style="color: rgb(181, 137, 0);">JsonReader</span> reader) <span class="hljs-keyword" style="color: rgb(133, 153, 0);">throws</span> <span class="hljs-type" style="color: rgb(181, 137, 0);">IOException</span> {
        <span class="hljs-comment" style="color: rgb(147, 161, 161);">// TODO Auto-generated method stub</span>
        <span class="hljs-keyword" style="color: rgb(133, 153, 0);">if</span> (reader.peek() == <span class="hljs-type" style="color: rgb(181, 137, 0);">JsonToken</span>.<span class="hljs-type" style="color: rgb(181, 137, 0);">NULL</span>) {
            reader.nextNull();
            <span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span> <span class="hljs-string" style="color: rgb(42, 161, 152);">""</span>;
        }
        <span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span> reader.nextString();
    }
    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    public void write(<span class="hljs-type" style="color: rgb(181, 137, 0);">JsonWriter</span> writer, <span class="hljs-type" style="color: rgb(181, 137, 0);">String</span> value) <span class="hljs-keyword" style="color: rgb(133, 153, 0);">throws</span> <span class="hljs-type" style="color: rgb(181, 137, 0);">IOException</span> {
        <span class="hljs-comment" style="color: rgb(147, 161, 161);">// TODO Auto-generated method stub</span>
        <span class="hljs-keyword" style="color: rgb(133, 153, 0);">if</span> (value == <span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span>) {
            writer.nullValue();//默认序列化时会忽略值为null的属性,如果需要该属性为“”则用write.value("");
            <span class="hljs-keyword" style="color: rgb(133, 153, 0);">return</span>;
        }
        writer.value(value);
    }
}</code>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值