首页 > 技术文章 > FastJson的一些使用

chenmc 2018-07-25 17:09 原文

前言

最近经常使用json的一些转换,使用的是fastjson,所以就对fastjson进行了一些汇总,记录下来。


正文

主要的api

首先是一些类库的说明:

  SerializeWriter:相当于StringBuffer

  JSONArray:相当于List<Object>

  JSONObject:相当于Map<String, Object>

  JSON反序列化没有真正数组,本质类型都是List<Object>

关于JSON类的一些api:

public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean 
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。

细节代码展示

1. json对象与String的互转

JSONObject jsonObject = new JSONObject(); //创建一个json对象
jsonObject.put("hello","hello");
jsonObject.put("world","world");
jsonObject.put("json","json");
String jsonString  = jsonObject.toJSONString();  //json转换成String
//打印结果如下:{"world":"world","json":"json","hello":"hello"}
//需要注意的是:put是在前面put进去。
String json = "{\"world\":\"world\",\"json\":\"json\",\"hello\":\"hello\"}";    //这是一个json字符串
JSONObject jsonObject = JSONObject.parseObject(json); //转换成json对象
System.out.println(jsonObject.toJSONString());       //{"world":"world","json":"json","hello":"hello"}
//添加一个新的字段到json对象中并打印
jsonObject.put("content","这是新添加进入的一个字段");
System.out.println(jsonObject.toJSONString());    //{"world":"world","json":"json","hello":"hello","content":"这是新添加进入的一个字段"}

2. json与javaBean的互转

User user = new User();
user.setPassword("111");
user.setUsername("222");
user.setCreateDate(new Date());
String jsonString = JSONObject.toJSONString(user);  //将javaBean序列化成json对象(javaBean中的date类型数据不需要格式化)
System.out.println(jsonString);     //{"createDate":1532495262966,"password":"111","username":"222"}
//如果时间需要格式化,可以使用下面的
String s = JSONObject.toJSONStringWithDateFormat(user, "yyyy-MM-dd");
System.out.println(s);      //{"createDate":"2018-07-25","password":"111","username":"222"}
//目前网上查到的可以识别的时间格式是:yyyy-MM-dd,yyyy-MM-dd HH:mm:ss,yyyy-MM-dd HH:mm:ss,毫秒数字,毫秒数字字符串,new Date(198293238)类型
//将对象字符串转换成javaBean
User parseUser = JSONObject.parseObject(s, User.class);
System.out.println(parseUser.toString());   //User{username='222', password='111', createDate=Wed Jul 25 00:00:00 CST 2018}

3. json与Map的互转

    Map<String,Object> map = new HashMap<>();
        map.put("hello","hello");
        map.put("world","world");
        map.put("json","json");
        String jsonString = JSON.toJSONString(map);
        String jsonStringPettyFormat = JSON.toJSONString(map,true);
        String jsonString1 = JSON.toJSONString(map);
        System.out.println(jsonString);     //{"world":"world","json":"json","hello":"hello"}
        System.out.println(jsonStringPettyFormat);  //带格式的json
       {
            "world":"world",
           "json":"json",
           "hello":"hello"
       }
        System.out.println(jsonString1);    //{"world":"world","json":"json","hello":"hello"}
        //将json转换成map
        Map map1 = JSON.parseObject(jsonString, Map.class);             //{world=world, json=json, hello=hello}
        Map map2 = JSON.parseObject(jsonStringPettyFormat, Map.class);  //{world=world, json=json, hello=hello}
        Map map3 = JSON.parseObject(jsonString1, Map.class);            //{world=world, json=json, hello=hello}

4. json与List集合的互转

List<User> userList = new ArrayList<>();
userList.add(new User("111","222",new Date()));
userList.add(new User("222","222",new Date()));
String jsonString = JSON.toJSONStringWithDateFormat(userList,"yyyy-MM-dd"); //[{"createDate":"2018-07-25","password":"222","username":"111"},{"createDate":"2018-07-25","password":"222","username":"222"}]
//将json转换成list
List<User> userList1 = JSON.parseArray(jsonString, User.class); //[User{username='111', password='222', createDate=Wed Jul 25 16:49:11 CST 2018}, User{username='222', password='222', createDate=Wed Jul 25 16:49:11 CST 2018}]
List<Map> maps = JSON.parseArray(jsonString, Map.class);        //[{password=222, createDate=2018-07-25, username=111}, {password=222, createDate=2018-07-25, username=222}]
       

特别的:可以将map转换成json然后转换成javaBean

推荐阅读