Java JSON使用总结
-
json 背景知识
- SourceForge.net 是全球最大开源软件开发平台和仓库,网站建立的宗旨,就是为开源软件提供一个存储、协作和发布的平台。SourceForge 上拥有大量非常优秀的开源软件
- net.sf.json和com.alibaba.fastjson对比 : fastjson 更快
- FastJson、Jackson、Gson、Json的效率简单对比
- NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别
net.sf.json
将NULL转换为一个“null” 字符串处理。如果直接对存在null值得JSON对象使用getString
进行操作,那么就容易出现使用getString("anull") == null
的操作,fastjson
则是将null直接转换为一个null
对象,所以可以直接使用getString("anull") == null
进行比较
-
net.sf.json-lib
-
pom 配置 : maven 仓库是 2010年上传的 ,上面有两个版本 13和 15 ,这里指定15
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
-
JSON 字符串 转成 Java对象
// 将json字符串转成Java对象 jsonObject = JSONObject.fromObject(json); Student stu = (Student) JSONObject.toBean(jsonObject, Student.class); //将json字符串转成Java的Array数组 jsonArray = JSONArray.fromObject(json); Object[] os = jsonArray.toArray(); Student[] stus = (Student[]) JSONArray.toArray(jsonArray, Student.class); // 将JSON字符串转成Java的List集合 List<Student> list = JSONArray.toList(jsonArray, Student.class); // 将json字符串转换成Collection接口 Collection<Student> con = JSONArray.toCollection(jsonArray, Student.class);
-
Java对象 转成 JSON 字符串
// 将JavaObject转换成JSON字符串 jsonObject = JSONObject.fromObject(student); jsonObject
-