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>