前端之家收集整理的这篇文章主要介绍了
FastJson 使用,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下载地址
package JsonDemo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
/** * 需要get和set方法才能json转换 * @author wwhhff11 * @since 2016/03/18 * @return */
class Outer {
int a = 0;
double b = 1.0;
String c = "123";
Date d = null;
class Inner {
int e = 10;
public int getE() {
return e;
}
public void setE(int e) {
this.e = e;
}
@Override
public String toString() {
return "Inner [e=" + e + "]";
}
}
Inner inner = new Inner();
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public Date getD() {
return d;
}
public void setD(Date d) {
this.d = d;
}
public Inner getInner() {
return inner;
}
public void setInner(Inner inner) {
this.inner = inner;
}
@Override
public String toString() {
return "Outer [a=" + a + ",b=" + b + ",c=" + c + ",d=" + d
+ ",inner=" + inner + "]";
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
String alibabaJson = JSON.toJSONString(outer);
System.out.println(alibabaJson);
String alibabaJsonNull = JSON.toJSONString(outer,SerializerFeature.WriteMapNullValue);
System.out.println(alibabaJsonNull);
String alibabaJsonPretty = JSON.toJSONString(outer,true);
System.out.println(alibabaJsonPretty);
Object obj=JSON.parse(alibabaJson);
System.out.println(obj);
System.out.println(obj instanceof Outer);
Outer outer2=JSON.parSEObject(alibabaJson,Outer.class);
System.out.println(outer2);
System.out.println(outer instanceof Outer);
List<Outer> list=new ArrayList<Outer>();
for(int i=0;i<2;i++){
list.add(new Outer());
}
String alibabaJson1 = JSON.toJSONString(list);
System.out.println(alibabaJson1);
List<Outer> list1=JSON.parseArray(alibabaJson1,Outer.class);
System.out.println(list1);
}
}
{"a":0,"b":1,"c":"123","inner":{"e":10}}
{"a":0,"d":null,"inner":{"e":10}}
{
"a":0,"inner":{
"e":10
}
}
{"b":1,"inner":{"e":10},"a":0}
false
Outer [a=0,b=1.0,c=123,d=null,inner=Inner [e=10]]
true
[{"a":0,"inner":{"e":10}},{"a":0,"inner":{"e":10}}]
[Outer [a=0,inner=Inner [e=10]],Outer [a=0,inner=Inner [e=10]]]
原文链接:https://www.f2er.com/json/289436.html