欢迎大家关注我的公众号【老周聊架构】,Java后端主流技术栈的原理、源码分析、架构以及各种互联网高并发、高性能、高可用的解决方案。
1、alibaba的JSONObject对象调用toJsonString方法直接转换
依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.41</version> </dependency>
代码:
RiemannUser riemannUser = new RiemannUser(); riemannU(1); riemannU("Hello JSONObject"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = (new Date()); riemannU(date); String jsonString = JSONObject.toJSONString(riemannUser); Sy(jsonString);
运行结果:
{"id":1,"message":"Hello JSONObject","sendDate":"2019-07-04 00:01:55"}
alibaba的JSONObject.toJSONString 的源码
public static String toJSONString(Object object) { return toJSONString(object, new SerializerFeature[0]); } public static String toJSONString(Object object, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for feature : features) { (feature, true); } (object); return out.toString(); } finally { out.close(); } }
2、net. 先调用 fromObject 再调用 toString
依赖:
<dependency> <groupId>net.;/groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
这里一定要加入<classifier>jdk15</classifier> 这一行,原因是:还关系到两个jdk版本的实现j和j。
代码:
RiemannUser riemannUser = new RiemannUser(); riemannU(1); riemannU("Hello JSONObject"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = (new Date()); riemannU(date); JSONObject jsonObject = JSONObject.fromObject(riemannUser); String jsonString = j(); Sy(jsonString);
运行结果:
{"id":1,"intList":[],"message":"Hello JSONObject","nodeName":"","sendDate":"2019-07-03 23:59:32"}
3、总结
通过这两种方式,不知道小伙伴们有没有发现什么秘密。
是的,运行结果不一样,第一种方式是得到set了的值,为空的值不会取出来。
相反,第二种方式得到是这个对象的所有属性,不管有值还是没有值。
public static JSONObject fromObject(Object object) { return fromObject(object, new JsonConfig()); } public static JSONObject fromObject(Object object, JsonConfig jsonConfig) { if (object != null && !JSONU(object)) { if (object instanceof Enum) { throw new JSONException("'object' is an Enum. Use JSONArray instead"); } else if (object instanceof Annotation || object != null && object.getClass().isAnnotation()) { throw new JSONException("'object' is an Annotation."); } else if (object instanceof JSONObject) { return _fromJSONObject((JSONObject)object, jsonConfig); } else if (object instanceof DynaBean) { return _fromDynaBean((DynaBean)object, jsonConfig); } else if (object instanceof JSONTokener) { return _fromJSONTokener((JSONTokener)object, jsonConfig); } else if (object instanceof JSONString) { return _fromJSONString((JSONString)object, jsonConfig); } else if (object instanceof Map) { return _fromMap((Map)object, jsonConfig); } else if (object instanceof String) { return _fromString((String)object, jsonConfig); } else if (!JSONU(object) && !JSONU(object) && !JSONU(object)) { if (object)) { throw new JSONException("'object' is an array. Use JSONArray instead"); } else { return _fromBean(object, jsonConfig); } } else { return new JSONObject(); } } else { return new JSONObject(true); } } private static JSONObject _fromString(String str, JsonConfig jsonConfig) { if (str != null && !"null".equals(str)) { return _fromJSONTokener(new JSONTokener(str), jsonConfig); } else { fireObjectStartEvent(jsonConfig); fireObjectEndEvent(jsonConfig); return new JSONObject(true); } } public JSONObject(boolean isNull) { this(); = isNull; }
从源码也可以看出,为空的字段,赋的空的值。