2-3-1 Java Web基础-JSON

JSON与JavaScript

JSON.parse()方法将字符串转换为JSON对象

1         var str = "{\"class_name\":\"五年级四班\"}";//str.ename
2         var json = JSON.parse(str);
3         console.log(str);
4         console.log(json);
5         document.write("班级:" + json.class_name);

JSON.stringify()方法将JSON对象转换为字符串

1     var json1 = {"class_name" : "五年级四班"};
2     var str1 = JSON.stringify(json1);
3     console.info(json1);
4     console.info(str1);
5     var json2 = {};
6     json2.class_name = "五年级五班";
7     json2.floor = "逸夫楼四层";
8     json2.teacher = "王义夫";
9     console.info(json2);

JSON与FastJSON

实体类

 1 import java.util.Date;
 2 
 3 import com.alibaba.fastjson.annotation.JSONField;
 4 
 5 public class Employee {
 6     private Integer empno;
 7     private String ename;
 8     private String job;
 9     //@JSONField(format="yyyy-MM-dd HH:mm:ss SSS")
10     @JSONField(name = "hiredate" , format="yyyy-MM-dd")
11     private Date hdate;
12     private Float salary;
13     @JSONField(serialize = false)
14     private String dname;
15     public Integer getEmpno() {
16         return empno;
17     }
18     public void setEmpno(Integer empno) {
19         this.empno = empno;
20     }
21     public String getEname() {
22         return ename;
23     }
24     public void setEname(String ename) {
25         this.ename = ename;
26     }
27     public String getJob() {
28         return job;
29     }
30     public void setJob(String job) {
31         this.job = job;
32     }
33     public Date getHdate() {
34         return hdate;
35     }
36     public void setHdate(Date hdate) {
37         this.hdate = hdate;
38     }
39     public Float getSalary() {
40         return salary;
41     }
42     public void setSalary(Float salary) {
43         this.salary = salary;
44     }
45     public String getDname() {
46         return dname;
47     }
48     public void setDname(String dname) {
49         this.dname = dname;
50     }
51     
52     
53 }
 1 import java.util.Calendar;
 2 
 3 import com.alibaba.fastjson.JSON;
 4 
 5 public class FastJsonSample1 {
 6     public static void main(String[] args) {
 7         Employee employee = new Employee();
 8         employee.setEmpno(4488);
 9         employee.setEname("王晓东");
10         employee.setJob("客户经理");
11         employee.setSalary(10000f);
12         employee.setDname("市场部");
13         Calendar c = Calendar.getInstance();
14         c.set(2019, 0, 30, 0, 0, 0);
15         employee.setHdate(c.getTime());
16         //FastJSON中提供了JSON对象,完成对象与JSON字符串的互相转换
17         String json = JSON.toJSONString(employee);
18         System.out.println(json);
19         Employee emp = JSON.parseObject(json, Employee.class);
20         System.out.println(emp.getEname());
21     }
22 }
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 import com.alibaba.fastjson.JSON;
 5 
 6 public class FastJsonSample2 {
 7     public static void main(String[] args) {
 8         List emplist = new ArrayList();
 9         for (int i = 1 ; i <= 100 ; i++) {
10             Employee employee = new Employee();
11             employee.setEmpno(4488 + i);
12             employee.setEname("员工" + i);
13             emplist.add(employee);
14         }
15         String json = JSON.toJSONString(emplist);
16         System.out.println(json);
17         List<Employee> emps = JSON.parseArray(json , Employee.class);
18         for(Employee e : emps) {
19             System.out.println(e.getEmpno() + ":" + e.getEname());
20         }
21     }
22 }

 

posted @ 2020-09-12 12:36  mingmingn  阅读(166)  评论(0)    收藏  举报