赞
踩
转载链接:http://blog.csdn.net/flysun3344/article/details/54707965
需要相关jar包,如果使用maven的话,在pom.xml文件加入一下依赖:
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.46</version>
- </dependency>
User.java
- package com.accord.fastjson;
-
- public class User {
- private String userName;
- private Integer age;
-
-
- public User() {
- super();
- }
-
- public User(String userName, Integer age) {
- super();
- this.userName = userName;
- this.age = age;
- }
-
- public String getUserName() {
- return userName;
- }
-
- public void setUserName(String userName) {
- this.userName = userName;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- }

FastJson.java
- package com.accord.fastjson;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.alibaba.fastjson.TypeReference;
- import com.alibaba.fastjson.parser.ParserConfig;
- import com.alibaba.fastjson.serializer.SerializerFeature;
-
- public class FastJson {
- public static void main(String[] args) {
- //基本的序列化
- 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);
-
- //将List<Map>转成JSON
- List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
- Map<String,Object> map1 = new HashMap<String, Object>();
- map1.put("key1", "One");
- map1.put("key2", "Two");
-
- Map<String,Object> map2 = new HashMap<String, Object>();
- map2.put("key1", "Three");
- map2.put("key2", "Four");
-
- list.add(map1);
- list.add(map2);
-
- String listJson = JSON.toJSONString(list);
- System.out.println(listJson);
-
-
- //自定义JavaBean User 转成Json
- User user = new User();
- user.setUserName("wjy");
- user.setAge(25);
- String userJson = JSON.toJSONString(user);
- System.out.println(userJson);
-
-
- //可以输出格式化后的JSON字符串
- //传入一个对象和一个布尔类型(是否格式化),将对象转成格式化后的JSON字符串。
- List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
- Map<String,Object> map1 = new HashMap<String, Object>();
- map1.put("key1", "One");
- map1.put("key2", "Two");
-
- Map<String,Object> map2 = new HashMap<String, Object>();
- map2.put("key1", "Three");
- map2.put("key2", "Four");
-
- list.add(map1);
- list.add(map2);
-
- String listJson = JSON.toJSONString(list,true);
- System.out.println(listJson);
-
-
- //日期格式化:
- //FastJSON可以直接对日期类型格式化,在缺省的情况下,FastJSON会将Date转成long
- String dataJson = JSON.toJSONString(new Date());
- System.out.println(dataJson); //结果:1401370199040
-
-
- //使用SerializerFeature特性格式化日期。
- String dateJson = JSON.toJSONString(new Date(),SerializerFeature.WriteDateUseDateFormat);
- System.out.println(dateJson); //结果:"2018-02-09 09:14:58"
-
-
- //指定输出日期格式
- String dateJson = JSON.toJSONStringWithDateFormat(new Date(),"yyyy-MM-dd");
- System.out.println(dateJson); //结果:"2018-02-09"
-
-
- //使用单引号
- List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
- Map<String,Object> map1 = new HashMap<String, Object>();
- map1.put("key1", "One");
- map1.put("key2", "Two");
-
- Map<String,Object> map2 = new HashMap<String, Object>();
- map2.put("key1", "Three");
- map2.put("key2", "Four");
-
- list.add(map1);
- list.add(map2);
- //String listJson = JSON.toJSONString(list,SerializerFeature.UseSingleQuotes);
- //System.out.println(listJson); //结果:[{'key2':'Two','key1':'One'},{'key2':'Four','key1':'Three'}]
-
- String listJson = JSON.toJSONString(list,SerializerFeature.PrettyFormat);
- System.out.println(listJson); //结果是带有格式的
-
-
- //输出Null字段。
- //缺省情况下FastJSON不输入为值Null的字段,可以使用SerializerFeature.WriteMapNullValue使其输出。
- Map<String, Object> map = new HashMap<String, Object>();
- String b = null;
- Integer i = 1;
- map.put("a", b);
- map.put("b", i);
- String listJson = JSON.toJSONString(map,SerializerFeature.WriteMapNullValue);
- System.out.println(listJson); //结果:{"b":1,"a":null}
-
- //序列化是写入类型信息
- User user = new User();
- user.setAge(26);
- user.setUserName("wjy");
- String listJson = JSON.toJSONString(user,SerializerFeature.WriteClassName);
- System.out.println(listJson); //结果:{"@type":"com.accord.fastjson.User","age":26,"userName":"wjy"}
-
- //将上面的反序列化
- ParserConfig.getGlobalInstance().setAutoTypeSupport(true); //不加这句会报错:
- /*Exception in thread "main" com.alibaba.fastjson.JSONException: autoType is not support. com.accord.fastjson.User
- at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1026)
- at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:316)
- at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1356)
- at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1322)
- at com.alibaba.fastjson.JSON.parse(JSON.java:152)
- at com.alibaba.fastjson.JSON.parse(JSON.java:162)
- at com.alibaba.fastjson.JSON.parse(JSON.java:131)
- at com.accord.fastjson.FastJson.main(FastJson.java:111)*/
- User user1 = (User)JSON.parse(listJson);
- System.out.println(user1.getUserName()); //结果:wjy
-
-
- //指定class信息反序列化
- User user = new User();
- user.setUserName("李四");
- user.setAge(24);
- String userJson = JSON.toJSONString(user);
- User user1 = JSON.parseObject(userJson,User.class);
- System.out.println(user1.getUserName());
-
- //集合反序列化
- List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
- Map<String, Object> map1 = new HashMap<String, Object>();
- map1.put("key1", "One");
- map1.put("key2", "Two");
- Map<String, Object> map2 = new HashMap<String, Object>();
- map2.put("key1", "Three");
- map2.put("key2", "Four");
- list.add(map1);
- list.add(map2);
- String listJson = JSON.toJSONString(list);
- List<Map> list1 = JSON.parseArray(listJson, Map.class);
- for (Map<String,Object> map : list1) {
- System.out.println(map.get("key1"));
- System.out.println(map.get("key2"));
- }
-
-
- //泛型的反序列化(使用TypeReference传入类型信息)
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("key1", "One");
- map.put("key2", "Two");
- String mapJson = JSON.toJSONString(map);
- Map<String,Object> map1 = JSON.parseObject(mapJson,new TypeReference<Map<String,Object>>(){});
- System.out.println(map1.get("key1"));
- System.out.println(map1.get("key2"));
-
- //将Map转成JSONObject,然后添加元素,输出
- Map<String,Object> map = new HashMap<String, Object>();
- map.put("key1", "One");
- map.put("key2", "Two");
-
- JSONObject j = new JSONObject(map);
- j.put("key3", "Three");
-
- System.out.println(j.get("key1"));
- System.out.println(j.get("key2"));
- System.out.println(j.get("key3"));
-
- //将List对象转成JSONArray,然后输出
- List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
-
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("key1", "One");
- map.put("key2", "Two");
-
- Map<String, Object> map2 = new HashMap<String, Object>();
- map2.put("key1", "Three");
- map2.put("key2", "Four");
-
- list.add(map);
- list.add(map2);
-
- JSONArray j = JSONArray.parseArray(JSON.toJSONString(list));
- for (int i = 0; i < j.size(); i++) {
- System.out.println(j.get(i));
- }
- }
- }

- package com.accord.fastjson;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.serializer.SerializerFeature;
-
- public class MyJsonUtil {
- private static final SerializerFeature[] features = {
- SerializerFeature.WriteMapNullValue, // 输出空置字段
- SerializerFeature.WriteNullListAsEmpty,
- // // list字段如果为null,输出为[],而不是null
- SerializerFeature.WriteNullNumberAsZero,
- // // 数值字段如果为null,输出为0,而不是null
- SerializerFeature.WriteNullBooleanAsFalse,
- // // Boolean字段如果为null,输出为false,而不是null
- SerializerFeature.WriteNullStringAsEmpty,
- // // 字符类型字段如果为null,输出为"",而不是null
- SerializerFeature.WriteDateUseDateFormat // 日期格式化yyyy-MM-dd HH:mm:ss
- };
-
- public static String toJson(Object object) {
- return JSON.toJSONString(object, features);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。