FastJson的常用操作

Posted 明-学以致用

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastJson的常用操作相关的知识,希望对你有一定的参考价值。

FastJson的常用操作

2017-06-05

常用操作包括以下内容:

  1. 对象与(JsonObject或JsonArray)与String的互换
  2. String转换为(JsonObject或JsonArray)如何保持顺序
  3. Eval的使用
  4. 值为null的(JsonObject或JsonArray)转换为String时如何保存

代码:

技术分享
package Demo.Sample1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class Sample {

    public static void main(String[] args) {
        
        Group group = GenerateGroup();
        String ss="{\"id\":1.1,\"name\":\"admin\",\"users\":[{\"id\":2.2,\"name\":\"guest\"},{\"id\":3.3,\"name\":\"root\"}]}";        
        Group groupWithNull=GenerateGroupWithNull();
        
        BasicObjectToStringCompare();
        JsonObjectToStringCompare(ss);
        NJsonObjectToStringCompare(group);
        JsonStringSequence(group, ss);        
        JsonEval(ss,"$.users");
        DealWithNullToStringString(groupWithNull);        
    }
    
    public static void BasicObjectToStringCompare()
    {
        System.out.println("---------- 基元类型 toString() toJSONString() 比较: ----------");
        System.out.println("ZifuChuan");
        System.out.println(JSON.toJSONString("ZifuChuan"));
        System.out.println(true);
        System.out.println(JSON.toJSONString(true));
        System.out.println(1.1);
        System.out.println(JSON.toJSONString(1.1));
    }
    
    public static String NJsonObjectToStringCompare(Group group) {
        String jsonString = JSON.toJSONString(group);
        System.out.println("---------- 非JsonObject toString() toJSONString() 比较: ----------");
        System.out.println(group.toString());
        System.out.println(JSON.toJSONString(group));
        return jsonString;
    }
    
    public static String JsonObjectToStringCompare(String ss) {
        Object o=JSON.parse(ss,Feature.OrderedField);
        System.out.println("---------- JsonObject toString() toJSONString() 比较: ----------");
        System.out.println(o.toString());
        System.out.println(JSON.toJSONString(o));
        return ss;
    }

    

    public static void JsonStringSequence(Group group, String jsonString) {
        System.out.println("---------- 是否有序: ----------");
        Object o=JSON.parse(jsonString);
        System.out.println(o.toString());;
        
        Object o1=JSON.parse(jsonString,Feature.OrderedField);
        System.out.println(o1.toString());
        
        Object o2 = JSON.toJSON(group);
        System.out.println(o2.toString());
    }
    
    public static void JsonEval(String ss,String jsonPath) {
        System.out.println("---------- Json Eval: ----------");        
        Object o3=JSONPath.eval(JSON.parse(ss,Feature.OrderedField),jsonPath);
        System.out.println(o3.toString());
    }

    public static void DealWithNullToStringString(Group groupWithNull) {
        System.out.println("---------- JsonObject with null emlement: ----------");
        System.out.println(JSON.toJSONString(groupWithNull));
        System.out.println(JSON.toJSONString(groupWithNull,SerializerFeature.WriteMapNullValue));
    }
    
    private static Group GenerateGroup() {
        Group group = new Group();
        group.setId(1.1);
        group.setName("admin");

        User guestUser = new User();
        guestUser.setId(2.2);
        guestUser.setName("guest");

        User rootUser = new User();
        rootUser.setId(3.3);
        rootUser.setName("root");

        group.addUser(guestUser);
        group.addUser(rootUser);
        return group;
    }
    
    private static Group GenerateGroupWithNull() {
        Group group = new Group();
        group.setId(1.1);
        group.setName("");

        User guestUser1 = new User();
        guestUser1.setId(null);
        guestUser1.setName("guest");

        User guestUser2 = new User();
        guestUser2.setId(3.3);
        
        User guestUser3 = new User();        

        group.addUser(guestUser1);
        group.addUser(guestUser2);
        group.addUser(guestUser3);
        return group;
    }
}
View Code

结果:

技术分享
---------- 基元类型 toString() toJSONString() 比较: ----------
ZifuChuan
"ZifuChuan"
true
true
1.1
1.1
---------- JsonObject toString() toJSONString() 比较: ----------
{"id":1.1,"name":"admin","users":[{"id":2.2,"name":"guest"},{"id":3.3,"name":"root"}]}
{"id":1.1,"name":"admin","users":[{"id":2.2,"name":"guest"},{"id":3.3,"name":"root"}]}
---------- 非JsonObject toString() toJSONString() 比较: ----------
Demo.Sample1.Group@23ab930d
{"id":1.1,"name":"admin","users":[{"id":2.2,"name":"guest"},{"id":3.3,"name":"root"}]}
---------- 是否有序: ----------
{"name":"admin","id":1.1,"users":[{"name":"guest","id":2.2},{"name":"root","id":3.3}]}
{"id":1.1,"name":"admin","users":[{"id":2.2,"name":"guest"},{"id":3.3,"name":"root"}]}
{"name":"admin","id":1.1,"users":[{"name":"guest","id":2.2},{"name":"root","id":3.3}]}
---------- Json Eval: ----------
[{"id":2.2,"name":"guest"},{"id":3.3,"name":"root"}]
---------- JsonObject with null emlement: ----------
{"id":1.1,"name":"","users":[{"name":"guest"},{"id":3.3},{}]}
{"id":1.1,"name":"","users":[{"id":null,"name":"guest"},{"id":3.3,"name":null},{"id":null,"name":null}]}
View Code

参考:

[1] Introducing JSON

[2] FastJson 教程

 

以上是关于FastJson的常用操作的主要内容,如果未能解决你的问题,请参考以下文章

FastJSON 常用操作

fastjson 操作Json常用格式数据

Fastjson常用方法

fastjason常用方法

提效小技巧——记录那些不常用的代码片段

fastjson使用详解