FastJson的常用操作

轉載:https://www.cnblogs.com/Ming8006/p/6944954.html

?

常用操作包括以下內容:

  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;}
}

  結果:

---------- 基元類型 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

http://www.json.org/

[2]?FastJson 教程

?https://www.w3cschool.cn/fastjson/%20

轉載于:https://www.cnblogs.com/MasterJ/p/7906309.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/252821.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/252821.shtml
英文地址,請注明出處:http://en.pswp.cn/news/252821.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Jenkins持續集成 之 Jenkins安裝

一、安裝JDK與TOMCAT8 參考地址: http://ibm.chick.blog.163.com/blog/static/144201610201652811537410/ 二、下載Jenkins安裝包 wget http://mirrors.jenkins.io/war/latest/jenkins.war 三、把jenkins.war放到TOMCAT下的webapps 四、啟動tomcat并訪問 http://10.…

夜視模式,多少猥瑣相機假汝之名

一般相機加裝一個紅外濾光片,就是一個可以用來干猥瑣事情的相機了。現在有相機直接把這個功能整合了,它的這個功能叫紅外夜視模式,可以拍下黑暗中的物體,當然,也可以透視紡織品和一些其他材質的衣物。這部相機具有500萬…

vue-router.esm.js?fe87:16 [vue-router] Route with name 'page' does not exist

本文地址&#xff1a;http://www.cnblogs.com/veinyin/p/7910525.html 我的路由配置 1 { 2 path: /page, 3 name: page, 4 component: page 5 } 我的 router-link <router-link :to"{name:page,params:{trans:ahahahaha}}">page</route…

Java中設計模式之單例設計模式-1

單例作用 1 節省內存2 可以避免多種狀態導致狀態沖突單例的創建步驟 1 私有化構造方法2 私有化聲明的屬性3 getInstance4 方法需要靜態單例分類 1.懶漢式 2.餓漢式 兩種單例區別&#xff1a; 餓漢式 線程安全的 懶漢式 線程不安全的 餓漢式&#xff1a; package 設計模式之單…

Vue 父子組件間的通信

前言 在 Vue 項目中父子組件的通信是非常常見的&#xff0c;最近做項目的時候發現對這方面的知識還不怎么熟練&#xff0c;在這邊做一下筆記&#xff0c;系統學習一下吧。 1 父組件傳值給子組件 1.1 傳值寫法 父組件傳值給子組件&#xff0c;這個就比較常見了&#xff0c;直接用…

夜視儀

國民兵所用F-16的駕駛艙&#xff0c;機員配有夜視鏡以備觀察領空的客機或飛行物體使用夜視鏡時看到的影像夜視儀〈night vision device&#xff0c;簡稱NVD〉&#xff0c;也稱夜視鏡&#xff0c;是透過光學科技強化黑暗中光源的科技裝置&#xff0c;通常用于軍事用途。目前在民…

【物理/數學】—— 概念的理解 moment、momentum

moment&#xff1a;矩&#xff0c;momentum&#xff1a;[物] 動量&#xff1b;動力&#xff1b;沖力&#xff1b; 數學意義上的 moment&#xff08;矩&#xff09;概念其實源自于物理范疇。首先我們來介紹物理學意義上的矩&#xff08;Momentum&#xff09;的概念。 1. 物理學意…

JAVA多線程實現的三種方式

JAVA多線程實現方式主要有三種&#xff1a;繼承Thread類、實現Runnable接口、使用ExecutorService、Callable、Future實現有返回結果的多線程。 其中前兩種方式線程執行完后都沒有返回值&#xff0c;只有最后一種是帶返回值的。 前兩種省略&#xff0c;著重看看第三種&#xff…

2springboot:快速創建springboot項目

使用IDEA快速創建springboot項目流程&#xff1a; 創建新的項目選擇 項目的命名以及包名 需要什么包就導入什么包 進行測試的單元 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId>&…

如何做好渠道銷售?

作者&#xff1a;路人李志強鏈接&#xff1a;https://www.zhihu.com/question/19689177/answer/28508799來源&#xff1a;知乎著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處。作為一名一線品牌的渠道銷售&#xff0c;如何讓代理商放棄其他小品…

2017浙江省賽 B - Problem Preparation ZOJ - 3959

地址&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode3959 題目&#xff1a; Its time to prepare the problems for the 14th Zhejiang Provincial Collegiate Programming Contest! Almost all members of Zhejiang University programming conte…

jQuery導入代碼片段并綁定事件

a.html <div><button class"button" >點我達</button> </div> b.html <html> <head><script type"text/javascript" src"./js/jquery-1.0.1.js"></script><meta http-equiv"Content-Ty…

驗證碼識別

基于Keras&#xff0c;識別簡單的4位字符的驗證碼。 https://github.com/ypwhs/captcha_break

LinCode落單的數

easy 落單的數 查看執行結果 60% 通過給出2*n 1 個的數字&#xff0c;除當中一個數字之外其它每一個數字均出現兩次。找到這個數字。 您在真實的面試中是否遇到過這個題&#xff1f; Yes例子 給出 [1,2,2,1,3,4,3]。返回 4 挑戰 一次遍歷&#xff0c;常數級的額外空間復雜度…

漢邦高科范俊峰:關于透霧攝像機那點事

慧聰安防網訊 生活在北方的人們&#xff0c;隔三差五就和霧霾來一次親密接觸&#xff0c;霧霾對人們的出行和交通有很大的影響&#xff0c;霧霾籠罩之下&#xff0c;人們出行如果闖紅燈、車輛違規行駛發生交通事故&#xff0c;交通監控攝像頭只能“霧里看花”&#xff0c;透霧攝…

SpringBoot注解大全(轉)

原文鏈接&#xff1a;[springBoot系列]--springBoot注解大全 一、注解(annotations)列表 SpringBootApplication&#xff1a;包含了ComponentScan、Configuration和EnableAutoConfiguration注解。其中ComponentScan讓spring Boot掃描到Configuration類并把它加入到程序上下文。…

祭奠我的2018

提前寫這篇文章&#xff0c;是因為深深的感覺到自己的無能 18年伊始&#xff0c;在浪潮干了大半年的我 還沉醉在深深的滿足感中&#xff0c;感覺自己對工作游刃有余 那種自大和自我滿足感充斥著我的心態 已經變得不會學習了 后來有一次機遇&#xff0c;讓我有了跳出舒適的機會 …

工業攝像頭傳感器尺寸與像元尺寸的關系

http://www.mv186.com/show-list.php?id462 在機器自動化行業檢測應用中&#xff0c;工業攝像頭是必備的硬件&#xff0c;而圖像傳感器又是工業攝像頭中最最關鍵核心的東西。圖像傳感器的尺寸&#xff08;靶面大小&#xff09;&#xff0c;往往直接關系到成像的質量。通常來講…

CF-798B

B. Mike and stringstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMike has n strings s1,?s2,?...,?sn each consisting of lowercase English letters. In one move he can choose a string si, erase the fi…

微信小程序倒計時實現

思路&#xff1a;跟一般js倒計時一樣&#xff0c;主要在于this的變相傳遞。 實現效果&#xff1a; wxml文件部分代碼&#xff1a; common.js文件 : 引用頁JS文件: PS: 1.在data里初始化時間格式&#xff0c;是避免時間加載的第1秒顯示為空白&#xff0c;因為setTimeOut是在1s以…