com.alibaba的fastjson简介
Fastjson简介
Fastjson是⼀个Java语⾔编写的⾼性能功能完善的JSON库。它采⽤⼀种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是⽬前Java语⾔中最快的JSON库。Fastjson接⼝简单易⽤,已经被⼴泛使⽤在缓存序列化、协议交互、Web输出、Android客户端等多种应⽤场景
Maven引⼊
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.37</version>
</dependency>
常⽤api
1. 将对象序列化成json字符串
....toJSONString( object)
2. 将json字符串反序列化成对象
<T> Project com.alibaba.fastjson.JSON.parseObject(String text, Class<T> clazz)
3. 将json字符串反序列化成JSON对象
JSONObject com.alibaba.fastjson.JSON.parseObject(String text)
4.根据key 得到json中的json数组
....getJSONArray( key)
5. 根据下标拿到json数组的json对象
....getJSONObject(int index)
6.. 根据key拿到json的字符串值
.
...getString( key)
7. 根据key拿到json的int值
int ....getIntValue( key)
8. 根据key拿到json的boolean值
boolean ....getBooleanValue( key)
实例说明
Project类
package com.json;
import java.util.List;
public class Project {
String pjName;
boolean waibao;
public boolean isWaibao() {
return waibao;
}
public void setWaibao(boolean waibao) {
this.waibao = waibao;
}
List<Factory> l_factory;
//List<Worker> worker;
public String getPjName() {
return pjName;
}
public void setPjName(String pjName) {
this.pjName = pjName;
}
public List<Factory> getL_factory() {
return l_factory;
}
public void setL_factory(List<Factory> l_factory) {
this.l_factory = l_factory;
}
}
Factory类
package com.json;
import java.util.List;
public class Factory {
String fcName;
List<Worker> l_worker;
public String getFcName() {
return fcName;
}
public void setFcName(String fcName) {
this.fcName = fcName;
}
public List<Worker> getL_worker() {
return l_worker;
}
public void setL_worker(List<Worker> l_worker) {
this.l_worker = l_worker;
}
}
Worker类
package com.json;
public class Worker {
String name;
String sex;
int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
测试类
package com.json;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class TestFastJson {
public static void main(String args[]) {
TestFastJson tfj = new TestFastJson();
Project prj = tfj.init();
String json= JsonString(prj);
System.out.println("json="+json);
//json={"l_factory":[{"fcName":"东软","l_worker":[{"age":30,"name":"乔佳飞","sex":"男"},{"age":25,"name":"李帅飞","sex":"⼥"}]},{"fcName":"亚信","l_worker":[{"age":26,"name":"王新峰","sex":"男"},{"age":0}]}],"pjName":"接⼝⾃动化","waibao":t        System.out.println("waibao="+JsonValueObj(json, "waibao", Boolean.class));
//waibao=true
JSONArray array = (JSONArray) JsonValueObj(json, "l_factory", JSONArray.class);
System.out.println("array="+String());
//array=[{"fcName":"东软","l_worker":[{"sex":"男","name":"乔佳飞","age":30},{"sex":"⼥","name":"李帅飞","age":25}]},{"fcName":"亚信","l_worker":[{"sex":"男","name":"王新峰","age":26},{"age":0}]}]
String jsonArr = JsonArrayValue(array, 0, "fcName");
System.out.println("fcName="+jsonArr);
//fcName=东软
JSONArray array2 = JsonArrayValueIsArray(array, 0, "l_worker");
System.out.println("array2="+String());
//array2=[{"sex":"男","name":"乔佳飞","age":30},{"sex":"⼥","name":"李帅飞","age":25}]
String json2 = JsonArrayValue(array2, 0);
System.out.println("json2="+json2);
//json2={"sex":"男","name":"乔佳飞","age":30}
/*以下输出
name=乔佳飞
sex=男
age=30
jsonArr2=男
* */
System.out.println("name="+JsonValueObj(json2, "name", String.class));
System.out.println("sex="+JsonValueObj(json2, "sex", String.class));
System.out.println("age="+JsonValueObj(json2, "age", Integer.class));
String jsonArr2 = JsonArrayValue(array2, 0, "sex");
System.out.println("jsonArr2="+jsonArr2);
/*以下输出
接⼝⾃动化
东软
乔佳飞
*/
System.out.JsonValue(json));
System.out.JsonValue(json,"l_factory"));
System.out.JsonValue(json,"l_factory","l_worker"));
}
public static void main1(String args[]) {
TestFastJson tfj = new TestFastJson();
Project prj = tfj.init();
String json= JsonString(prj);
prj.setPjName("序列化后修改pjname");
System.out.PjName());//序列化后修改pjname
Project po = JSON.parseObject(json,Project.class);
System.out.PjName());//接⼝⾃动化
}
public void tt(Class clazz) {
System.out.SimpleName());
Name().equals("String")) {
System.out.println("stringllala");
}
}
public Project init() {
Project pj = new Project();
Factory ft1 = new Factory();
Factory ft2 = new Factory();
Worker wk1 = new Worker();
wk1.setName("乔佳飞");
wk1.setSex("男");
wk1.setAge(30);
Worker wk2 = new Worker();
wk2.setName("李帅飞");
wk2.setSex("⼥");
wk2.setAge(25);
Worker wk3 = new Worker();
wk3.setName("魏晓博");
wk3.setSex("男");
wk3.setAge(27);
Worker wk4 = new Worker();
wk3.setName("王新峰");
wk3.setSex("男");
wk3.setAge(26);
List<Worker> workers1 = new ArrayList<Worker>();
workers1.add(wk1);
workers1.add(wk2);
List<Worker> workers2 = new ArrayList<Worker>();
workers2.add(wk3);
workers2.add(wk4);
ft1.setFcName("东软");
ft1.setL_worker(workers1);
ft2.setFcName("亚信");
ft2.setL_worker(workers2);
List<Factory> factorys = new ArrayList<Factory>();
factorys.add(ft1);
factorys.add(ft2);
pj.setPjName("接⼝⾃动化");
pj.setWaibao(true);
pj.setL_factory(factorys);
return pj;
}
/**
*
* 将对象转换成json
* */
public String getJsonString(Object obj) {
String json= JSONString(obj);
return json;
}
/
**
* 根据key得到json的value
* */
public String getJsonValue(String json) {
JSONObject jo = JSON.parseObject(json);
String value = jo.getString("pjName");
return value;
}
/**
* 根据key得到json的集合
* */
public JSONArray getJsonArray(String json, String key) {
JSONObject jo = JSON.parseObject(json);
JSONArray array = jo.getJSONArray(key);
return array;
}
/**
* 根据下标得到json数组的值
* */
public String getJsonArrayValue(JSONArray array, int index) {
JSONObject jo_fc = JSONObject(index);
String json = JSONString();
return json;
}
/**
* 根据下标得到json数组的值,再根据key得到该值的value,该值类型是String
* */
public String getJsonArrayValue(JSONArray array, int index, String key) {
JSONObject jo_fc = JSONObject(index);
String value = String(key);
return value;
}
/**
* 根据下标得到json数组的值,再根据key得到该值的value,该值类型是JSONArray
* */
public JSONArray getJsonArrayValueIsArray(JSONArray array, int index, String key) {        JSONObject jo_fc = JSONObject(index);
JSONArray arrayNew = JSONArray(key);
return arrayNew;
}
/**
* 根据对象的类型,⾃动识别获取该对象的值
* */
public Object getJsonValueObj(String json, String key, Class clazz) {
JSONObject jo = JSON.parseObject(json);
SimpleName().equals("String")) {
String value = jo.getString(key);
return value;
}else SimpleName().equals("Integer")) {
Integer value = jo.getInteger(key);
return value;
}else if (SimpleName().equals("Boolean")) {
Boolean value = jo.getBoolean(key);
return value;
}else SimpleName().equals("JSONArray")) {
JSONArray array = jo.getJSONArray(key);
return array;
}
else {
return "error, 暂不⽀持的类型:"+String();
}
}
public String getJsonValue(String json, String key) {
JSONObject jo = JSON.parseObject(json);
JSONArray array = jo.getJSONArray(key);
JSONObject jo_fc = JSONObject(0);
String value = String("fcName");
return value;
}
public String getJsonValue(String json, String key, String keyW) {
JSONObject jo = JSON.parseObject(json);
JSONArray array = jo.getJSONArray(key);
JSONObject jo_fc = JSONObject(0);
JSONArray arrayW = JSONArray(keyW);        JSONObject jo_wk = JSONObject(0);        String value = String("name");
int age = IntValue("age");
//System.out.println(age);
return value;
fastjson字符串转数组}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。