Fastjson 简述
- fastjson 是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean
- FastJson与Google的Gson都是解析Json的强者,两者不相伯仲
- FastJson 免费开源,GitHut地址:https://github.com/alibaba/fastjson
fastjson的优点
速度快
- fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。
使用广泛
- fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受。在2012年被开源中国评选为最受欢迎的国产开源软件之一。
测试完备
- fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个。每次发布都会进行回归测试,保证质量稳定。
使用简单
- fastjson的API十分简洁。
String text = JSON.toJSONString(obj); //序列化
VO vo = JSON.parseObject("{...}", VO.class); //反序列化
功能完备
支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。
下载和使用
- 可以在maven中央仓库中直接下载:
http://repo1.maven.org/maven2/com/alibaba/fastjson/
- 或者配置maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>x.x.x</version>
</dependency>
- 其中x.x.x是版本号,根据需要使用特定版本,建议使用最新版本。
android版本
- fastjson会不定期发布针对android版本优化的版本,android优化版本是去掉不必要的代码,减少体积,功能和标准版本基本一样。 已发布的android版本包括:
http://repo1.maven.org/maven2/com/alibaba/fastjson/1.1.51.android/
FastJson 解析
- 它的Api其实是Google的Gson基本类似,都是一通百通
- fastJson 中 JsonObject 类继承Json抽象类
Map 转 JSON字符串
/** * 将Map转Json字符串 */ @Test public void test1() { Map<String, Object> map = new HashMap<String, Object>(); map.put("key1", "One"); map.put("key2", "Two"); String mapJson = JSON.toJSONString(map); System.out.println(mapJson);//输出:{"key1":"One","key2":"Two"} }
POJO List 转 JSON字符串
/** * 将Java Bean List转Json字符串 */ @Test public void test6() { Person person1 = new Person(); person1.setName("张三"); person1.setAge(28); person1.setBirthday(new Date()); Person person2 = new Person(); person2.setName("李四"); person2.setAge(25); person2.setBirthday(new Date()); List<Person> persons = new ArrayList<Person>(); persons.add(person1); persons.add(person2); String object = JSON.toJSONString(persons); System.out.println(object); /**输出如下: * [{"age":28,"birthday":1530511546991,"name":"张三"},{"age":25,"birthday":1530511546991,"name":"李四"}] */ }
Json字符串转JsonObject
/** * 将Json字符串转为JsonObject对象 * 取值不存在时,返回null,使用Gson会抛异常 */ @Test public void test2() { String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); System.out.println(jsonObject.getString("key1"));//输出one System.out.println(jsonObject.getInteger("key2"));//输出110 System.out.println(jsonObject.getString("key3"));//输出null }
JsonObject转Json字符串
/** * 将JsonObject对象转为Json字符串 * 取值不存在时,返回null,使用Gson会抛异常 */ @Test public void test3() { String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); System.out.println(jsonObject.getString("key1"));//输出:one System.out.println(jsonObject.getInteger("key2"));//输出:110 System.out.println(jsonObject.getString("key3"));//输出:null String parserJsonStr = JSONObject.toJSONString(jsonObject); System.out.println(parserJsonStr);//输出:{"key1":"One","key2":"110"} }
JSONArray添加JSONObject
/** * 将JsonObject添加到JsonArray */ @Test public void test4() { JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("name", "张三"); jsonObject1.put("age", 25); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("name", "李四"); jsonObject2.put("age", 28); JSONArray jsonArray = new JSONArray(); jsonArray.add(jsonObject1); jsonArray.add(jsonObject2); String jsonArrStr = JSONArray.toJSONString(jsonArray); System.out.println(jsonArrStr);//输出:[{"name":"张三","age":25},{"name":"李四","age":28}] }
Json数组字符串转JsonArray
/** * 将Json数组字符串转JsonArray */ @Test public void test5() { String jsonArrStr = "[{\"name\":\"张三\",\"age\":25},{\"name\":\"李四\",\"age\":28}]"; JSONArray jsonArray = JSONArray.parseArray(jsonArrStr); for (Object object : jsonArray) { JSONObject jsonObject = (JSONObject) object; System.out.println(jsonObject.getString("name")); System.out.println(jsonObject.getString("age")); System.out.println("--------------"); } }
结果输出:
张三
25--------------
李四
28
--------------
Process finished with exit code 0
POJO 转Json字符串
/** * 将Java Bean 转Json字符串 */ @Test public void test7_2() { Person person1 = new Person(); person1.setName("张三"); person1.setAge(26); person1.setBirthday(new Date()); /**两种方式都行 * 因为JSONObject继承了JSON*/ String object = JSONObject.toJSONString(person1); /*String object = JSON.toJSONString(person1);*/ System.out.println(object); /**输出如下: * {"age":26,"birthday":1530511790302,"name":"张三"} */ }
POJO 转 JsonObject
/** * 将Java Bean 转Json 对象 */ @Test public void test8() { Person person1 = new Person(); person1.setName("张三"); person1.setAge(28); person1.setBirthday(new Date()); /**方式一*/ String jsonStr = JSONObject.toJSONString(person1); JSONObject jsonObject = JSONObject.parseObject(jsonStr); System.out.println(jsonObject.get("name"));//输出:张三 /**方式二*/ JSONObject jsonObject1 = (JSONObject)JSONObject.toJSON(person1); System.out.println(jsonObject1.get("age"));//输出:28 }
POJO List转 JsonArray
/** * 将Java Bean List 转Json 数组 */ @Test public void test9() { Person person1 = new Person(); person1.setName("张三"); person1.setAge(28); person1.setBirthday(new Date()); Person person2 = new Person(); person2.setName("李四"); person2.setAge(25); person2.setBirthday(new Date()); List<Person> persons = new ArrayList<Person>(); persons.add(person1); persons.add(person2); /**方式1*/ String jsonArrStr = JSONArray.toJSONString(persons); JSONArray jsonArray = JSONArray.parseArray(jsonArrStr); JSONObject jsonObject1 = (JSONObject)jsonArray.get(0); System.out.println(jsonObject1.get("name"));//输出:张三 /**方式2*/ JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(persons); JSONObject jsonObject2 = (JSONObject)jsonArray1.get(1); System.out.println(jsonObject2.get("name"));//输出:李四 }
JsonObject 转 POJO
/** * 将Json 对象 转 Java Bean */ @Test public void test10() { Person person1 = new Person(); person1.setName("张三"); person1.setAge(28); person1.setBirthday(new Date()); String jsonPOJOStr = JSON.toJSONString(person1); Person person = JSONObject.parseObject(jsonPOJOStr, Person.class); System.out.println(person); /** * 输出:Person{age=28, name='张三', birthday=Mon Jul 02 14:27:53 CST 2018} */ }
JsonArray 转 POJO List
/** * 将Json 数组 转 Java List */ @Test public void test11() { String jsonArrPOJOStr = "[{\"birthday\":1530512954968,\"name\":\"张三\",\"age\":28}," + "{\"birthday\":1530512954968,\"name\":\"李四\",\"age\":25}]"; List<Person> personList = JSONArray.parseArray(jsonArrPOJOStr, Person.class); for (Person person : personList) { System.out.println(person); } /** * 输出: * Person{age=28, name='张三', birthday=Mon Jul 02 14:29:14 CST 2018} * Person{age=25, name='李四', birthday=Mon Jul 02 14:29:14 CST 2018} */ }