Fastjson示例代码

前端之家收集整理的这篇文章主要介绍了Fastjson示例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
packagecom.wzc.test.Test;

importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;

importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONArray;
importcom.alibaba.fastjson.JSONObject;
importcom.alibaba.fastjson.TypeReference;
importcom.alibaba.fastjson.serializer.SerializerFeature;

classUser{
	privateintid;

	privateStringname;

	privateDatebirthday;

	publicintgetId(){
		returnid;
	}

	publicvoidsetId(intid){
		this.id=id;
	}

	publicStringgetName(){
		returnname;
	}

	publicvoidsetName(Stringname){
		this.name=name;
	}

	publicDategetBirthday(){
		returnbirthday;
	}

	publicvoidsetBirthday(Datebirthday){
		this.birthday=birthday;
	}

	@Override
	publicStringtoString(){
		SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-dd");
		return"User[id="+id+",name="+name+",birthday="+format.format(birthday)+"]";
	}
}

publicclassApp{
	publicstaticvoidmain(String[]args){
		//序列化与反序列Bean
		//因为Bean中的字段带有类型,不同的类型都能正确处理
		//1.准备数据
		Useruser=newUser();
		user.setId(1);
		user.setName("User");
		user.setBirthday(newDate());
		//2.序列化
		StringbeanStr=JSON.toJSONString(user);
		System.out.println(beanStr);
		System.out.println();
		//3.反序列化
		Userbean=JSON.parSEObject(beanStr,User.class);
		System.out.println(bean);
		System.out.println();
		//4.格式化形式的反序列化
		StringprettyStr=JSON.toJSONString(user,SerializerFeature.PrettyFormat);
		System.out.println(prettyStr);
		System.out.println();
		//5.使用参数保留类型信息
		StringclassStr=JSON.toJSONString(user,SerializerFeature.WriteClassName,SerializerFeature.PrettyFormat);
		System.out.println(classStr);
		System.out.println();

		//序列化与反序列化Map
		//1.准备数据
		Map<String,Object>map=newHashMap<>();
		map.put("id",1);
		map.put("name","User");
		map.put("birthday",newDate());
		//2.序列化
		StringmapStr=JSON.toJSONString(map);
		System.out.println(mapStr);
		System.out.println();
		//3.反序列化
		//使用TypeReference提供类型信息
		//由于Value为Object类型,可以看到序列化和反序列化丢失了类型信息
		Map<String,Object>m=JSON.parSEObject(mapStr,newTypeReference<Map<String,Object>>(){
		});
		System.out.println(m);
		System.out.println();

		//序列化与反序列化数组
		//1.准备数据
		Useruser2=newUser();
		user2.setId(2);
		user2.setName("User2");
		user2.setBirthday(newDate());
		User[]array=newUser[]{user,user2};
		//2.序列化
		StringarrayStr=JSON.toJSONString(array);
		System.out.println(arrayStr);
		System.out.println();
		//3.反序列化
		List<User>users=JSON.parseArray(arrayStr,User.class);
		System.out.println(users);
		System.out.println();
		//4.使用JSONObject和JSONArray
		JSONArrayjsonArray=JSON.parseArray(arrayStr);
		for(inti=0;i<jsonArray.size();i++){
			JSONObjectobj=jsonArray.getJSONObject(i);
			System.out.println(obj);
		}
	}
}

输出

{"birthday":1453037936843,"id":1,"name":"User"}

User[id=1,name=User,birthday=2016-01-17]

{
	"birthday":1453037936843,"name":"User"
}

{
	"@type":"com.wzc.test.Test.User","birthday":1453037936843,"name":"User"
}

{"birthday":1453037937054,"name":"User","id":1}

{birthday=1453037937054,id=1}

[{"birthday":1453037936843,"name":"User"},{"birthday":1453037937057,"id":2,"name":"User2"}]

[User[id=1,birthday=2016-01-17],User[id=2,name=User2,birthday=2016-01-17]]

{"birthday":1453037936843,"id":1}
{"birthday":1453037937057,"name":"User2","id":2}
原文链接:https://www.f2er.com/json/289499.html

猜你在找的Json相关文章