JSON数据解析(三)——FastJson

来源:互联网 发布:淘宝太平鸟男装官方店 编辑:程序博客网 时间:2024/05/06 20:46

FastJson是阿里巴巴的JSON处理工具包。在使用过程中要求javabean 必须要包含一个无参的构造方法,有标准的get*** , set****方法,当然 在使用之前先导入jar包。

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class FastDemo {  
  2.     public static void main(String[] args) {  
  3.           
  4.         String str = "{no:1,name:'android',employees:[{name:'zhangsan',age:20},{name:'lisi',age:21},{name:'wangwu',age:22}]}";  
  5.           
  6.         Dept dept = JSON.parseObject(str,Dept.class);  
  7.           
  8.         System.out.println(dept);  
  9.           
  10.     }  
  11. }  

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class Dept {  
  2. private int no;  
  3.       
  4.     private String name;  
  5.       
  6.     private ArrayList<Employees> employees;  
  7.   
  8.     public int getNo() {  
  9.         return no;  
  10.     }  
  11.   
  12.     public void setNo(int no) {  
  13.         this.no = no;  
  14.     }  
  15.   
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.   
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.   
  24.     public ArrayList<Employees> getEmployees() {  
  25.         return employees;  
  26.     }  
  27.   
  28.     public void setEmployees(ArrayList<Employees> employees) {  
  29.         this.employees = employees;  
  30.     }  
  31.       
  32.     public Dept() {  
  33.         // TODO Auto-generated constructor stub  
  34.     }  
  35.   
  36.     @Override  
  37.     public String toString() {  
  38.         return "Dept [no=" + no + ", name=" + name + ", employees=" + employees  
  39.                 + "]";  
  40.     }  
  41.       
  42. }  
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class Employees {  
  2. private String name;  
  3.       
  4.     private int age;  
  5.       
  6.     public Employees() {  
  7.         // TODO Auto-generated constructor stub  
  8.     }  
  9.   
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.   
  14.     public void setName(String name) {  
  15.         this.name = name;  
  16.     }  
  17.   
  18.     public int getAge() {  
  19.         return age;  
  20.     }  
  21.   
  22.     public void setAge(int age) {  
  23.         this.age = age;  
  24.     }  
  25.   
  26.     @Override  
  27.     public String toString() {  
  28.         return "Employees [name=" + name + ", age=" + age + "]";  
  29.     }  
  30.       
  31. }  
0 0
原创粉丝点击