up:: JSON序列化与反序列化


但是在实际业务中,往往会是对多个对象批量处理:这个用处会非常广泛,以前做项目的时候也遇到过几次;

JSON序列化与反序列化用途,比如,

(1)要求:本地有100个员工对象信息,如何把这100个员工的信息传递给另外一个系统?

方法:可以将100个员工的信息JSON序列化,然后传递给另外一个系统;另一个系统接受收后,JSON反序列化成员工对象即可;

(2)后面Ajax中也会用到JSON序列化


一:多个对象的集合,转成JSON(数组)

String jsonString = JSON.toJSONString(empList) :还是这个方法,将对象集合作为参数传入即可:

 
    import java.util.ArrayList;
    import java.util.List;
 
    import com.alibaba.fastjson.JSON;
 
    public class FastJsonSample2 {
    	public static void main(String[] args) {
    		List<Employee> empList = new ArrayList<Employee>();
 
    		for(int i=1;i<=100;i++) {   // 简单模拟一下100个员工;只设置了两个属性;
    			Employee employee = new Employee();
    			employee.setEmpno(4488+i);
    			employee.setEname("员工"+i);
    			empList.add(employee);
    		}
 
    		String jsonString = JSON.toJSONString(empList);
    		System.out.println(jsonString);
    	}
 
 
    }

输出:发现(1)[ ]说明这是一个集合;(2)只有empno和ename,说明当某个属性的属性值为null时,则FastJson会将其忽略;

 
    [{"empno":4489,"ename":"员工1"},{"empno":4490,"ename":"员工2"},{"empno":4491,"ename":"员工3"},{"empno":4492,"ename":"员工4"},{"empno":4493,"ename":"员工5"},{"empno":4494,"ename":"员工6"},{"empno":4495,"ename":"员工7"},{"empno":4496,"ename":"员工8"},{"empno":4497,"ename":"员工9"},{"empno":4498,"ename":"员工10"},{"empno":4499,"ename":"员工11"},{"empno":4500,"ename":"员工12"},{"empno":4501,"ename":"员工13"},{"empno":4502,"ename":"员工14"},{"empno":4503,"ename":"员工15"},{"empno":4504,"ename":"员工16"},{"empno":4505,"ename":"员工17"},{"empno":4506,"ename":"员工18"},{"empno":4507,"ename":"员工19"},{"empno":4508,"ename":"员工20"},{"empno":4509,"ename":"员工21"},{"empno":4510,"ename":"员工22"},{"empno":4511,"ename":"员工23"},{"empno":4512,"ename":"员工24"},{"empno":4513,"ename":"员工25"},{"empno":4514,"ename":"员工26"},{"empno":4515,"ename":"员工27"},{"empno":4516,"ename":"员工28"},{"empno":4517,"ename":"员工29"},{"empno":4518,"ename":"员工30"},{"empno":4519,"ename":"员工31"},{"empno":4520,"ename":"员工32"},{"empno":4521,"ename":"员工33"},{"empno":4522,"ename":"员工34"},{"empno":4523,"ename":"员工35"},{"empno":4524,"ename":"员工36"},{"empno":4525,"ename":"员工37"},{"empno":4526,"ename":"员工38"},{"empno":4527,"ename":"员工39"},{"empno":4528,"ename":"员工40"},{"empno":4529,"ename":"员工41"},{"empno":4530,"ename":"员工42"},{"empno":4531,"ename":"员工43"},{"empno":4532,"ename":"员工44"},{"empno":4533,"ename":"员工45"},{"empno":4534,"ename":"员工46"},{"empno":4535,"ename":"员工47"},{"empno":4536,"ename":"员工48"},{"empno":4537,"ename":"员工49"},{"empno":4538,"ename":"员工50"},{"empno":4539,"ename":"员工51"},{"empno":4540,"ename":"员工52"},{"empno":4541,"ename":"员工53"},{"empno":4542,"ename":"员工54"},{"empno":4543,"ename":"员工55"},{"empno":4544,"ename":"员工56"},{"empno":4545,"ename":"员工57"},{"empno":4546,"ename":"员工58"},{"empno":4547,"ename":"员工59"},{"empno":4548,"ename":"员工60"},{"empno":4549,"ename":"员工61"},{"empno":4550,"ename":"员工62"},{"empno":4551,"ename":"员工63"},{"empno":4552,"ename":"员工64"},{"empno":4553,"ename":"员工65"},{"empno":4554,"ename":"员工66"},{"empno":4555,"ename":"员工67"},{"empno":4556,"ename":"员工68"},{"empno":4557,"ename":"员工69"},{"empno":4558,"ename":"员工70"},{"empno":4559,"ename":"员工71"},{"empno":4560,"ename":"员工72"},{"empno":4561,"ename":"员工73"},{"empno":4562,"ename":"员工74"},{"empno":4563,"ename":"员工75"},{"empno":4564,"ename":"员工76"},{"empno":4565,"ename":"员工77"},{"empno":4566,"ename":"员工78"},{"empno":4567,"ename":"员工79"},{"empno":4568,"ename":"员工80"},{"empno":4569,"ename":"员工81"},{"empno":4570,"ename":"员工82"},{"empno":4571,"ename":"员工83"},{"empno":4572,"ename":"员工84"},{"empno":4573,"ename":"员工85"},{"empno":4574,"ename":"员工86"},{"empno":4575,"ename":"员工87"},{"empno":4576,"ename":"员工88"},{"empno":4577,"ename":"员工89"},{"empno":4578,"ename":"员工90"},{"empno":4579,"ename":"员工91"},{"empno":4580,"ename":"员工92"},{"empno":4581,"ename":"员工93"},{"empno":4582,"ename":"员工94"},{"empno":4583,"ename":"员工95"},{"empno":4584,"ename":"员工96"},{"empno":4585,"ename":"员工97"},{"empno":4586,"ename":"员工98"},{"empno":4587,"ename":"员工99"},{"empno":4588,"ename":"员工100"}]
 

二:将JSON数组转成对象集合

List<Employee>istEmployee = JSON.parseArray(jsonString, Employee.class) ;:将JSON数组,转成对象集合

 
    import java.util.ArrayList;
    import java.util.List;
 
    import com.alibaba.fastjson.JSON;
 
    public class FastJsonSample2 {
    	public static void main(String[] args) {
    		List<Employee> empList = new ArrayList<Employee>();
 
    		for(int i=1;i<=100;i++) {   // 简单模拟一下100个员工;只设置了两个属性;
    			Employee employee = new Employee();
    			employee.setEmpno(4488+i);
    			employee.setEname("员工"+i);
    			empList.add(employee);
    		}
 
    		String jsonString = JSON.toJSONString(empList);
    		System.out.println(jsonString);
 
    		List<Employee> listEmployee = JSON.parseArray(jsonString, Employee.class);
    		for(Employee e:listEmployee) {
    			System.out.println(e.getEmpno()+":"+e.getEname());
    		}
    	}
 
    }
 

输出: