简单的gson对boolean转换的test(作为备注)
来源:互联网 发布:java数组的逆序输出 编辑:程序博客网 时间:2024/06/12 14:56
GsonBooleanTest.java
package com.test;import java.io.IOException;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.JsonSyntaxException;import com.google.gson.TypeAdapter;import com.google.gson.stream.JsonReader;import com.google.gson.stream.JsonToken;import com.google.gson.stream.JsonWriter;/** * @author John Kenrinus Lee * @version 2016-08-19 */public class GsonBooleanTest { public static void main(String[] args) { Mod mod = null; // see isTd String xx1 = "{\"isTd\":\"0\", \"mDd\":\"hello\"}"; // false String xx2 = "{\"isTd\":\"1\", \"mDd\":\"hello\"}"; // false String xx3 = "{\"isTd\":true, \"mDd\":\"hello\"}"; // true String xx4 = "{\"isTd\":\"true\", \"mDd\":\"hello\"}"; // true String xx5 = "{\"isTd\":false, \"mDd\":\"hello\"}"; // false String xx6 = "{\"isTd\":\"false\", \"mDd\":\"hello\"}"; // false String xx7 = "{\"isTd\":1, \"mDd\":\"hello\"}"; // throw; if use adapter, then true String xx8 = "{\"isTd\":0, \"mDd\":\"hello\"}"; // throw; if use adapter, then false String xx9 = "{\"isTd\":\"Yes\", \"mDd\":\"hello\"}"; // false String xx10 = "{\"isTd\":\"No\", \"mDd\":\"hello\"}"; // false Gson gson = new Gson(); mod = gson.fromJson(xx1, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx2, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx3, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx4, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx5, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx6, Mod.class); System.out.println(mod.isTd); gson = new GsonBuilder().registerTypeAdapter(Mod.class, new TypeAdapter<Mod>() { @Override public void write(JsonWriter out, Mod value) throws IOException { out.beginObject(); out.name("isTd").value(value.isTd); out.name("mDd").value(value.mDd); out.endObject(); } @Override public Mod read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } in.beginObject(); final Mod mod = new Mod(); while (in.hasNext()) { switch (in.nextName()) { case "isTd": if (in.peek() == JsonToken.BOOLEAN) { mod.isTd = in.nextBoolean(); } else if (in.peek() == JsonToken.NUMBER) { mod.isTd = in.nextInt() != 0; } else if (in.peek() == JsonToken.STRING) { String bool = in.nextString(); try { if (bool.equalsIgnoreCase("true") || bool.equalsIgnoreCase("yes") || Integer.parseInt(bool) != 0) { mod.isTd = true; } } catch (Exception ignored) { // Do nothing } } else { throw new JsonSyntaxException("Unknown type for isTd"); } break; case "mDd": mod.mDd = in.nextString(); break; } } in.endObject(); return mod; } }).create(); mod = gson.fromJson(xx7, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx8, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx9, Mod.class); System.out.println(mod.isTd); mod = gson.fromJson(xx10, Mod.class); System.out.println(mod.isTd); }}
Mod.java
public class Mod { public boolean isTd; public String mDd;}
php中0, 1, ios中Yes, No, 可能都算是boolean, 但在java的gson中转换失灵, 需要adapter.
0 0
- 简单的gson对boolean转换的test(作为备注)
- GSON对java对象的相互转换
- Gson解析boolean值的注意
- 简单的Gson转换json数据
- 简单的Boolean源码
- 作为初学者对c3p0的简单理解
- Struts2的属性boolean值的转换和封装和MyEclipse对boolean值自动添加get(),set()
- gson的简单介绍
- Gson的简单用法
- gson的简单应用
- Gson的简单使用
- Gson的简单使用
- Gson的简单使用
- 简单的Gson使用
- Gson的简单用法
- Gson的简单使用
- Gson的简单使用
- Gson的简单使用
- Petrozavodsk Winter-2013. Ural FU Contest
- hive高级查询
- poj 2031 Building a Space Station
- finally块中包含return语句对返回值的影响
- 自己写的LinkList
- 简单的gson对boolean转换的test(作为备注)
- 【HDU】1159 - Common Subsequence(LCS)
- git add , git commit 添加错文件 撤销
- Simple Line Editor----CSU 1019
- ios json解析3840错误
- 源码角度解析:StringBuffer、StringBuilder、String
- iTween
- POJ 2031 (最小生成树)
- Ubuntu下编译Android版本的ffmepg so库及源码