FastJson的常用操作
2017-06-05
常用操作包括以下内容:
- 对象与(JsonObject或JsonArray)与String的互换
- String转换为(JsonObject或JsonArray)如何保持顺序
- Eval的使用
- 值为null的(JsonObject或JsonArray)转换为String时如何保存
代码:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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 {</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) { Group group </span>=<span style="color: #000000;"> GenerateGroup(); String ss</span>="{"id":1.1,"name":"admin","users":[{"id":2.2,"name":"guest"},{"id":3.3,"name":"root"}]}"<span style="color: #000000;">; Group groupWithNull</span>=<span style="color: #000000;">GenerateGroupWithNull(); BasicObjectToStringCompare(); JsonObjectToStringCompare(ss); NJsonObjectToStringCompare(group); JsonStringSequence(group, ss); JsonEval(ss,</span>"$.users"<span style="color: #000000;">); DealWithNullToStringString(groupWithNull); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> BasicObjectToStringCompare() { System.out.println(</span>"---------- 基元类型 toString() toJSONString() 比较: ----------"<span style="color: #000000;">); System.out.println(</span>"ZifuChuan"<span style="color: #000000;">); System.out.println(JSON.toJSONString(</span>"ZifuChuan"<span style="color: #000000;">)); System.out.println(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">); System.out.println(JSON.toJSONString(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">)); System.out.println(</span>1.1<span style="color: #000000;">); System.out.println(JSON.toJSONString(</span>1.1<span style="color: #000000;">)); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String NJsonObjectToStringCompare(Group group) { String jsonString </span>=<span style="color: #000000;"> JSON.toJSONString(group); System.out.println(</span>"---------- 非JsonObject toString() toJSONString() 比较: ----------"<span style="color: #000000;">); System.out.println(group.toString()); System.out.println(JSON.toJSONString(group)); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> jsonString; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String JsonObjectToStringCompare(String ss) { Object o</span>=<span style="color: #000000;">JSON.parse(ss,Feature.OrderedField); System.out.println(</span>"---------- JsonObject toString() toJSONString() 比较: ----------"<span style="color: #000000;">); System.out.println(o.toString()); System.out.println(JSON.toJSONString(o)); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ss; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> JsonStringSequence(Group group, String jsonString) { System.out.println(</span>"---------- 是否有序: ----------"<span style="color: #000000;">); Object o</span>=<span style="color: #000000;">JSON.parse(jsonString); System.out.println(o.toString());; Object o1</span>=<span style="color: #000000;">JSON.parse(jsonString,Feature.OrderedField); System.out.println(o1.toString()); Object o2 </span>=<span style="color: #000000;"> JSON.toJSON(group); System.out.println(o2.toString()); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> JsonEval(String ss,String jsonPath) { System.out.println(</span>"---------- Json Eval: ----------"<span style="color: #000000;">); Object o3</span>=<span style="color: #000000;">JSONPath.eval(JSON.parse(ss,Feature.OrderedField),jsonPath); System.out.println(o3.toString()); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> DealWithNullToStringString(Group groupWithNull) { System.out.println(</span>"---------- JsonObject with null emlement: ----------"<span style="color: #000000;">); System.out.println(JSON.toJSONString(groupWithNull)); System.out.println(JSON.toJSONString(groupWithNull,SerializerFeature.WriteMapNullValue)); } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> Group GenerateGroup() { Group group </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Group(); group.setId(</span>1.1<span style="color: #000000;">); group.setName(</span>"admin"<span style="color: #000000;">); User guestUser </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> User(); guestUser.setId(</span>2.2<span style="color: #000000;">); guestUser.setName(</span>"guest"<span style="color: #000000;">); User rootUser </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> User(); rootUser.setId(</span>3.3<span style="color: #000000;">); rootUser.setName(</span>"root"<span style="color: #000000;">); group.addUser(guestUser); group.addUser(rootUser); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> group; } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> Group GenerateGroupWithNull() { Group group </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Group(); group.setId(</span>1.1<span style="color: #000000;">); group.setName(</span>""<span style="color: #000000;">); User guestUser1 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> User(); guestUser1.setId(</span><span style="color: #0000ff;">null</span><span style="color: #000000;">); guestUser1.setName(</span>"guest"<span style="color: #000000;">); User guestUser2 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> User(); guestUser2.setId(</span>3.3<span style="color: #000000;">); User guestUser3 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> User(); group.addUser(guestUser1); group.addUser(guestUser2); group.addUser(guestUser3); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> group; }
}
结果:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
---------- 基元类型 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}]}
参考:
[1] Introducing JSON
[2] FastJson 教程