FastJson简单使用
首先建立两个实体类,Student.java 和 Teacher.java
public class Student {
private int id;
private String name;
private int age;
/**
* 默认的构造方法必须不能省,不然不能解析
*/
public Student(){
}
public Student(int id,String name,int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
public class Teacher {
private int id;
private String name;
private List<Student> students;
/**
* 默认的构造方法必须不能省,不然不能解析
*/
public Teacher() {
}
public Teacher(int id,String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", mStudents="
+ students + "]";
}
}
对象转为json串
public class App
{
public static void main(String[] args) {
Student student = new Student(0, "Aaron", 24);
System.out.println(JSON.toJSONString(student)); List<Student> students = new ArrayList<Student>();
for(int i=0;i<5;i++) {
Student stu = new Student(i, "Student" + i, 18 +i);
students.add(stu);
}
System.out.println(JSON.toJSONString(students)); List<Teacher> teaList = new ArrayList<Teacher>();
long time = System.currentTimeMillis();
for(int i=0;i<10;i++) {
Teacher teacher = new Teacher(i, "Teacher " + i);
List<Student> stus = new ArrayList<Student>();
for(int j = 0 ;j<4;j++) {
Student s = new Student(j, "Student" + j, 18 +j);
stus.add(s);
}
teacher.setStudents(stus);
teaList.add(teacher);
}
String jsonTeach = JSON.toJSONString(teaList);
System.out.println("fastjson = " + jsonTeach);
System.out.println("==========================================");
Student student1 = new Student(0, "Aaron", 24);
System.out.println(JSON.toJSONString(student1,true)); }
}
json串转为对象
public class TestParseToObject {
public static void main(String[] args) {
Student student = new Student(0, "Aaron", 24);
String str = JSON.toJSONString(student,true);
System.out.println(JSON.parseObject(str,Student.class));
System.out.println("=================================================");
List<Student> students = new ArrayList<Student>();
for(int i=0;i<5;i++) {
Student stu = new Student(i, "Student" + i, 18 +i);
students.add(stu);
}
// 过滤哪些属性需要转换
// SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age");
// String jsonStu =JSON.toJSONString(students,filter);
String jsonStu = JSON.toJSONString(students);
System.out.println(jsonStu);
// List<Student> stu =JSON.parseObject(jsonStu, new TypeReference<List<Student>>(){});
List<Student> stu =JSON.parseArray(jsonStu, Student.class);
for(int i=0;i<stu.size();i++)
{
System.out.println(stu.get(i));
}
}
}
日期相关
1.日期格式化:
FastJSON可以直接对日期类型格式化,在缺省的情况下,FastJSON会将Date转成long。
例5:FastJSON将java.util.Date转成long。
1 String dateJson = JSON.toJSONString(new Date());
2
3 System.out.println(dateJson);
输出结果:
1401370199040
例6:使用SerializerFeature特性格式化日期。
1 String dateJson = JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat);
2
3 System.out.println(dateJson);
输出结果:
"2014-05-29 21:36:24"
也可以指定输出日期格式。
例7:指定输出日期格式。
1 String dateJson = JSON.toJSONStringWithDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
2
3 System.out.println(dateJson);
输出结果:
"2014-05-29 21:47:00.154"
2.使用单引号。
例8:以例2为例。
String listJson = JSON.toJSONString(list, SerializerFeature.UseSingleQuotes);
输出结果:
[{'key1':'One','key2':'Two'},{'key3':'Three','key4':'Four'}]
3.JSON格式化。
例9:
String listJson = JSON.toJSONString(list, SerializerFeature.PrettyFormat);
输出结果:与例4结果一致。
4.输出Null字段。
缺省情况下FastJSON不输入为值Null的字段,可以使用SerializerFeature.WriteMapNullValue使其输出。
例10:

1 Map<String, Object> map = new HashMap<String,Object>();
2
3 String b = null;
4 Integer i = 1;
5
6 map.put("a", b);
7 map.put("b", i);
8
9 String listJson = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);

输出结果:
{"a":null,"b":1}
JSONObject,JSONArray是JSON的两个子类
JSONObject相当于Map<String, Object>,
JSONArray相当于List<Object>。
简单方法示例:
例16:将Map转成JSONObject,然后添加元素,输出。

1 Map<String, Object> map = new HashMap<String, Object>();
2 map.put("key1", "One");
3 map.put("key2", "Two");
4
5 JSONObject j = new JSONObject(map);
6
7 j.put("key3", "Three");
8
9 System.out.println(j.get("key1"));
10 System.out.println(j.get("key2"));
11 System.out.println(j.get("key3"));

输出结果:
1 One
2 Two
3 Three
例17:将List对象转成JSONArray,然后输出。

1 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
2
3 Map<String, Object> map = new HashMap<String, Object>();
4 map.put("key1", "One");
5 map.put("key2", "Two");
6
7 Map<String, Object> map2 = new HashMap<String, Object>();
8 map2.put("key1", "Three");
9 map2.put("key2", "Four");
10
11 list.add(map);
12 list.add(map2);
13
14 JSONArray j = JSONArray.parseArray(JSON.toJSONString(list));
15
16 for(int i=0; i<j.size(); i++){
17 System.out.println(j.get(i));
18 }

输出结果:
1 {"key1":"One","key2":"Two"}
2 {"key1":"Three","key2":"Four"}
FastJson简单使用的更多相关文章
- FastJSON 简单使用
FastJSON是一个Java语言编写的高性能,功能完善,完全支持http://json.org的标准的JSON库.多了不说了,百度一下一大把. 在此,简单的总结一下自己用过,测试过的方法. 如果使用 ...
- Fastjson简单使用方法
一.简单数据的序列化 pubic class UserInfo implements Serializable{ private String name; private int age; publi ...
- Jackson和fastjson简单用法及性能对比
背景: Java处理JSON数据有三个比较流行的类库FastJSON.Gson和Jackson.fastjson是阿里做的国有开源Java工具包,jackson是spring mvc内置的json转换 ...
- fastjson 简单使用 及其JSONObject使用
阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...
- fastjson简单使用demo,@JSONField注解属性字段上与set、get方法上。实体类toString(),实体类转json的区别;_下划线-减号大小写智能匹配
一.demo代码 @JSONField注解属性字段上与set.get方法上.使用@Data注解(lombok插件安装最下方),对属性“笔名”[pseudonym]手动重写setter/getter方法 ...
- fastJson简单实用
public class FastJsonText { @Test public void text(){ User user1 = new User(); user1.setName("健 ...
- 高性能JSON库---FastJson(阿里巴巴)
1.FastJSON简单介绍 Fastjson是一个Java语言编写的高性能功能完好的JSON库. 它採用一种"假定有序高速匹配"的算法,把JSON Parse的性能提升到极致,是 ...
- Fastjson反序列化漏洞研究
0x01 Brief Description java处理JSON数据有三个比较流行的类库,gson(google维护).jackson.以及今天的主角fastjson,fastjson是阿里巴巴一个 ...
- JSON 解析 (一)—— FastJSON的使用
FastJSON是一个高性能.功能完善的json序列化和解析工具库,可使用Maven添加依赖 <dependency> <groupId>com.alibaba</gro ...
随机推荐
- 将DataSet转化成XML格式的String类型,再转化回来。
/// <summary> /// 获取DataSet的Xml格式 /// </summary> public static string GetDataSetXml(this ...
- android自定义View的绘制原理
每天我们都会使用很多的应用程序,尽管他们有不同的约定,但大多数应用的设计是非常相似的.这就是为什么许多客户要求使用一些其他应用程序没有的设计,使得应用程序显得独特和不同. 如果功能布局要求非常定制化, ...
- 嵌入式Linux引导过程之1.6——Xloader的Xloader_Entry
我们已经看完了XLOADER_ENTRY里调用的前两个标号的代码,分别是sys_init和ddr_init.对于一个嵌入式系统来说,这两 个部分的代码是在一开始就执行的,至少是在从bootrom里面的 ...
- 3.1 PCI设备BAR空间的初始化
在PCI Agent设备进行数据传送之前,系统软件需要初始化PCI Agent设备的BAR0~5寄存器和PCI桥的Base.Limit寄存器.系统软件使用DFS算法对PCI总线进行遍历时,完成这些寄存 ...
- 笔记︱范数正则化L0、L1、L2-岭回归&Lasso回归(稀疏与特征工程)
机器学习中的范数规则化之(一)L0.L1与L2范数 博客的学习笔记,对一些要点进行摘录.规则化也有其他名称,比如统计学术中比较多的叫做增加惩罚项:还有现在比较多的正则化. -------------- ...
- 通过grub-install命令把grub安装到u盘-总结
通过grub-install命令把grub安装到u盘 ①准备一个u盘,容量不限,能有1MB都足够了. ②把u盘格式化(我把u盘格式化成FAT.fat32格式了,最后证明也是成功的).③开启linux系 ...
- [PHP开发] phpmailer问题 错误原因: Could not instantiate mail function
Send via the PHP mail() function function mail_send($header, $body) { // Create mail recipient list ...
- 双硬盘RAID 0全攻略
. RAID53 RAID7即高效数据传送磁盘结构,是RAID3和带区结构的统一,因此它速度比较快,也有容错功能.但价格十分高,不易于实现. 为什么需要磁盘阵列 如何增加磁盘的存取(ac ...
- E: 未发现软件包 install_flash_player_11_linux.x86_64.tar.gz
1 错误描述 youhaidong@youhaidong:~$ sudo apt-get install install_flash_player_11_linux.x86_64.tar.gz 正在读 ...
- Educational Codeforces Round37 E - Connected Components?
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> ...