Fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。
主要特点:- 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)
- 强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
- 零依赖(没有依赖其它任何类库除了JDK)
1.下载地址:https://github.com/alibaba/fastjson/archive/1.2.4.zip
2.FastJson主要类
JSON | 主要类,一般使用静态方法,序列化java bean序列化为json文本或者json字符串反序列化为相应对象 |
JSONObject | Json对象 |
JSONReader | JsonReader,可以在本地读取或者网络读取数据并可以反序列化 |
JSONWriter | JsonWriter,可以将对象序列化Json字符串,写入本地或者发送至服务器 |
TypeReference | #类型引用在反序列化化很常用 |
3.Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
<span style="font-size:12px;">public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray public static final JSONObject parSEObject(String text); // 把JSON文本parse成JSONObject public static final T parSEObject(String text,Class clazz); // 把JSON文本parse为JavaBean public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray public static final List parseArray(String text,Class clazz); //把JSON文本parse成JavaBean集合 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 public static final String toJSONString(Object object,boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。</span>
public static void main(String[] args) throws IOException,Exception {
/*
* 1.将指定的 JavaBean对象解析成Json字符串
*/
Person p1 = new Person("zhangsan",22,1332117);
// 字段的get属性
System.out.println(JSON.toJSONString(p1));
/*
* 2.将封装了javaBean对象的集合解析成json字符串,并对该数据进行过滤
*/
ValueFilter valueFilter = new ValueFilter() {
@Override
public Object process(Object source,String name,Object value) {
if (value.equals("lisi")) {
return " 159785";
}
return value;
}
};
List list = new ArrayList();
list.add(new Person("lisi",19,159785));
list.add(new Person("wangwu",10,113123213));
list.add(new Person("zhaoliu",29,1444785));
System.out.println(JSON.toJSONString(list,valueFilter));
/*
* 3.将List>集合 转换为json字符串
*/
List> list2 = new ArrayList>();
Map map1 = new HashMap();
map1.put("001",new Person("wangwu",159785));
map1.put("002",new Person("lisi",159785));
map1.put("003",159785));
Map map2 = new HashMap();
map2.put("001",new Person("zhaoliu",159785));
map2.put("002",159785));
map2.put("003",159785));
list2.add(map1);
list2.add(map2);
System.out.println(JSON.toJSONString(list2));
/*
* 4.将指定的字符串解析成 指定的javaBean对象(使用泛型)
*/
// 字段的set属性
Person p2 = JSON.parSEObject(Person.getUtil(1),Person.class);
/*
* 5.将指定的字符串解析成 封装了指定javaBean对象的集合
*/
// 方法一:
List list1 = JSON.parseArray(Person.getUtil(2),Person.class);
// 方法二:
List list4 = JSON.parSEObject(Person.getUtil(2),new TypeReference>(){});
/*
* 6.将json字符串 解析成 List>集合 ,注意Type类型的定义
*/
// 关键type类型
List> list3 = JSON.parSEObject(Person.getUtil(3),new TypeReference>>(){});
/*
* 7.将json字符串解析为 JsonObject对象, 由于该对象继承了map,可以得到键值对
*/
JSONObject object = (JSONObject) JSON.parse(Person.getUtil(1));
System.out.println("name:" + object.getString("name"));
System.out.println("age:" + object.getIntValue("age"));
System.out.println("number:" + object.getIntValue("number"));
/*
* 8.name过滤器,只能对name进行更新,不管是否显示
*/
NameFilter filter = new NameFilter() {
@Override
public String process(Object source,Object value) {
// source是当前对象, name是key, value实在值
if ("age".equals(name)) {
return "AGE";
}
return name;
// {"age":22,"name":"zhangsan","number":1332117} //过滤前
// {"AGE":22,"number":1332117} //过滤后
}
};
JSONSerializer jsonSerializer = new JSONSerializer();
// 通过增加一个过滤器,为name和值进行过滤
jsonSerializer.getNameFilters().add(filter);
jsonSerializer.write(p1);
System.out.println(jsonSerializer.toString());
/*
* 9.属性过滤器PropertyFilter,满足要求的可以不做显示
*/
PropertyFilter propertyFilter = new PropertyFilter() {
@Override
public boolean apply(Object source,Object value) {
if ("age".equals(name)) {
return true;
}
return false;
}
};
JSONSerializer jsonSerializer2 = new JSONSerializer();
jsonSerializer2.getPropertyFilters().add(propertyFilter);
jsonSerializer2.write(list);
System.out.println(jsonSerializer2.toString());
/*
* 10.值过滤器ValueFilter,对满足要求的可以不做显示
*/
ValueFilter valueFilter2 = new ValueFilter() {
@Override
public Object process(Object source,Object value) {
if (value.equals(10)) {
return null;
}
return value;
}
};
JSONSerializer jsonSerializer3 = new JSONSerializer();
jsonSerializer3.getValueFilters().add(valueFilter2);
jsonSerializer3.write(list);
System.out.println(jsonSerializer3.toString());
/*
* 11.在本地路径下读取文件的json字符串信息,得到数据并赋值javaBean对象
*/
JSONReader reader = new JSONReader(new FileReader("C:\\Test.txt"));
// 注意type的使用
List list10 = reader.readObject(new TypeReference>() {}.getType());
reader.close();
/*
* 12.将Object对象保存至本地路径中,保存为json字符串
*/
JSONWriter writer = new JSONWriter(new FileWriter("c:\\Test.txt"));
writer.writeObject(list10);
writer.close();
/*
* 13.在网络上获取json数据,并保存为对应的javaBean对象信息
*/
List list11 = null;
URL url = new URL("http://192.168.117.114:8080/Test/My");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
// 通过JsonReader类得到发出的输出流对象
JSONReader reader2 = new JSONReader(new InputStreamReader(in));
// 得到Object对象
list11 = reader2.readObject(new TypeReference>(){}.getType());
}
for (Person p : list11) {
System.out.println(p);
}
/*
* 14.将客户端的javaBean对象,上传至服务器
*/
byte[] bytes = JSON.toJSONBytes(list11);
URL url2 = new URL("http://192.168.117.114:8080/Test/My");
HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
conn2.setRequestMethod("POST");
conn2.setConnectTimeout(3000);
conn2.setRequestProperty("content-length",String.valueOf(bytes.length));
conn2.setRequestProperty("content-type","application/x-www-form-urlencoded");
conn2.setDoOutput(true);
OutputStream os = conn2.getOutputStream();
os.write(bytes,bytes.length);
os.close();
if (conn2.getResponseCode() == 200) {
System.out.println("上传成功! ");
}
}
{CSDN:CODE:
public class class Person {
public static String getUtil(int type) {
switch (type) {
case 1:
return new String(
"{\"age\":22,\"name\":\"zhangsan\",\"number\":1332117}");
break;
case 2:
return new String(
"[{\"age\":19,\"name\":\"lisi\",\"number\":159785},"
+ "{\"age\":10,\"name\":\"wangwu\",\"number\":113123213},"
+ "{\"age\":29,\"name\":\"zhaoliu\",\"number\":1444785}]");
break;
case 3:
return new String(
"[{\"001\":{\"age\":19,"
+ "\"002\":{\"age\":19,"
+ "\"003\":{\"age\":19,\"number\":159785}},"
+ "{\"001\":{\"age\":23,\"number\":159785}}]");
break;
default :
return null;
break;
}
private String name;
private int age;
private int number;
public Person() {
super();
}
public Person(String name,int age,int number) {
super();
this.name = name;
this.age = age;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String toString() {
return "Person [age=" + age + ",name=" + name + ",number=" + number + "]";
}
}
}
}