欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Fastjson

程序员文章站 2022-03-03 14:49:24
...
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

public class FastjsonTest {

static Map<String,Object> m=new HashMap<String,Object>();
static Person p=new FastjsonTest().new Person();
static List<Person> list=new ArrayList<Person>();
static Clazz cz=new FastjsonTest().new Clazz();


public static void main(String[] args) {
// toString(m);

// toString(p);

// toString(list);

// toString(cz);

// parse("");

// parse("",null);

parse("",null,null);
}

static void toString(Map<String,Object> m){
//String res=JSON.toJSONString(m);
String res=JSON.toJSONString(m,mapping);
System.out.println(res);
}

static void toString(Person p){
String res=JSON.toJSONString(p,mapping);
System.out.println(res);
}

static void toString(List<Person> list){
String res=JSON.toJSONString(list,mapping);
System.out.println(res);
}

static void toString(Clazz cz){
String res=JSON.toJSONString(cz,mapping);
System.out.println(res);
}

static void parse(String json){
String js="{\'bir\':\'2020-04-15 12:02:07\'}";
Map m=JSONObject.parseObject(js, Map.class);
System.out.println(m.get("bir"));
}

static void parse(String json,Class c){
String js="[{\'age\':66,\'birth\':\'2020-04-15 12:02:07\',\'name\':\'lisi0\'},{\'age\':67,\'birth\':\'2020-04-15 12:02:07\',\'name\':\'lisi1\'}]";
List list=JSONObject.parseArray(js, Map.class);
System.out.println(list.size());
}

static void parse(String json,Class c,String s){
String js="{\'cname\':\'一班\',\'persons\':[{\'age\':66,\'birth\':\'2020-04-15 12:02:07\',\'name\':\'lisi0\'},{\'age\':67,\'birth\':\'2020-04-15 12:02:07\',\'name\':\'lisi1\'}]}";
Map mm=JSONObject.parseObject(js, Map.class);
CA<PersonA> cc=JSONObject.parseObject(js, new TypeReference<CA<PersonA>>(){});
System.out.println(cc.getPersons().size());
}
//测试数据
static {
m.put("name", "王五");
m.put("age", 22);
m.put("bir", new Date()); //日期

p.setName("lisi");
p.setAge(66);
p.setBirth(new Date());

for(int i=0;i<2;i++){
Person per=new FastjsonTest().new Person();
per.setName("lisi"+i);
per.setAge(66+i);
per.setBirth(new Date());
list.add(per);
}

cz.setCname("一班");
cz.setPersons(list);
}

//对日期的序列化
static SerializeConfig mapping = new SerializeConfig(); 
static String dateFormat; 
static { 
    dateFormat = "yyyy-MM-dd HH:mm:ss"; 
    mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat)); 
}

class Person{
String name;
int age;
Date birth;
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}

class Clazz{
String cname;
List<Person> persons;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
}

class PersonA{
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class CA<T>{
String cname;
List<T> persons;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public List<T> getPersons() {
return persons;
}
public void setPersons(List<T> persons) {
this.persons = persons;
}
}
相关标签: json