淺談fastjson的常用使用方法
如下所示:
package Demo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import entity.Userinfo;
/**
* fastjson 是一個(gè)性能很好的 Java 語言實(shí)現(xiàn)的 JSON 解析器和生成器,來自阿里巴巴的工程師開發(fā)。 主要特點(diǎn):
* 1.快速FAST(比其它任何基于Java的解析器和生成器更快,包括jackson) 強(qiáng)大(支持普通JDK類包括任意Java Bean
* 2.Class、Collection、Map、Date或enum) 零依賴(沒有依賴其它任何類庫除了JDK)
*
*/
public class TestFastJson {
public static void main(String[] args) {
String json = "{\"name\":\"chenggang\",\"age\":24}";
String arrayAyy = "[[\'馬云',50],null,[\'馬化騰',30]]";
// Entity2json("zhangsan", 24);
// list2Json();
Complexdata();
// Deserialization(json);
// DateFormate(new Date());
// Json2Eetity(json);
// String2JSONArray(arrayAyy);
}
// 實(shí)體轉(zhuǎn)為Json
public static void Entity2json(String name, int age) {
Userinfo info = new Userinfo(name, age);
String str_json = JSON.toJSONString(info); //
System.out.println("實(shí)體轉(zhuǎn)化為Json" + str_json);
}
// list轉(zhuǎn)Json
public static void list2Json() {
List<Userinfo> list = new ArrayList<Userinfo>();
Userinfo userinfo1 = new Userinfo("lisi", 15);
Userinfo userinfo2 = new Userinfo("wangwu", 16);
list.add(userinfo1);
list.add(userinfo2);
String json = JSON.toJSONString(list, true);
System.out.println("List集合轉(zhuǎn)json格式字符串 :" + json);
}
// 字符數(shù)組轉(zhuǎn)化為JSon
private static void String2JSONArray(String arrayAyy) {
JSONArray array = JSONArray.parseArray(arrayAyy);
System.out.println("數(shù)組:" + array);
System.out.println("數(shù)組長度: " + array.size());
Collection nuCon = new Vector();
nuCon.add(null);
array.removeAll(nuCon);
System.out.println("數(shù)組:" + array);
System.out.println("數(shù)組長度: " + array.size());
}
// 復(fù)雜數(shù)據(jù)類型
public static void Complexdata() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("username", "zhangsan");
map.put("age", 24);
map.put("sex", "男");
// map集合
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", "xiaohong");
temp.put("age", "23");
map.put("girlInfo", temp);
// list集合
List<String> list = new ArrayList<String>();
list.add("爬山");
list.add("騎車");
list.add("旅游");
map.put("hobby", list);
String jsonString = JSON.toJSONString(map);
System.out.println("復(fù)雜數(shù)據(jù)類型:" + jsonString);
}
public static void Deserialization(String json) {
Userinfo userInfo = JSON.parseObject(json, Userinfo.class);
System.out.println("姓名是:" + userInfo.getName() + ", 年齡是:"
+ userInfo.getAge());
}
// 格式話日期
public static void DateFormate(Date date) {
System.out.println("輸出毫秒值:" + JSON.toJSONString(date));
System.out.println("默認(rèn)格式為:"
+ JSON.toJSONString(date,
SerializerFeature.WriteDateUseDateFormat));
System.out.println("自定義日期:"
+ JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd",
SerializerFeature.WriteDateUseDateFormat));
}
// Json轉(zhuǎn)為實(shí)體
private static void Json2Eetity(String json) {
Userinfo userInfo = JSON.parseObject(json, Userinfo.class);
System.out.println("輸出對象的地址:" + userInfo.toString());
System.out.println("輸出對象的名字:" + userInfo.getName());
}
}
以上Demo所用到的實(shí)體類:
package entity;
public class Userinfo {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Userinfo() {
super();
}
public Userinfo(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
以上這篇淺談fastjson的常用使用方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的 BigDecimal 和 String 的相互轉(zhuǎn)換問題
這篇文章主要介紹了Java中的 BigDecimal 和 String 的相互轉(zhuǎn)換問題,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
Presto自定義函數(shù)@SqlNullable引發(fā)問題詳解
這篇文章主要為大家介紹了Presto自定義函數(shù)@SqlNullable引發(fā)問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
java 線程方法join簡單用法實(shí)例總結(jié)
這篇文章主要介紹了java 線程方法join簡單用法,結(jié)合實(shí)例形式總結(jié)分析了Java線程join方法的功能、原理及使用技巧,需要的朋友可以參考下2019-11-11
HttpServletRequestWrapper干預(yù)Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請求的流程中干預(yù)?Request對象,?通過基于HttpServletRequestWrapper和?Filter組合進(jìn)行干預(yù),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09
Elasticsearch學(xué)習(xí)之Terms?set?查詢
這篇文章主要為大家介紹了Elasticsearch學(xué)習(xí)Terms?set?查詢示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
java.lang.OutOfMemoryError: Metaspace異常解決的方法
這篇文章主要介紹了java.lang.OutOfMemoryError: Metaspace異常解決的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
IDEA解決src和resource下創(chuàng)建多級目錄的操作
這篇文章主要介紹了IDEA解決src和resource下創(chuàng)建多級目錄的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
在java中main函數(shù)如何調(diào)用外部非static方法
這篇文章主要介紹了在java中main函數(shù)如何調(diào)用外部非static方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

