zoukankan      html  css  js  c++  java
  • JSON

    JSON必须键值对
    慕课网Json学习笔记 加油吧

     1.JSON介绍

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta charset="UTF-8">
     5 <title>Insert title here</title>
     6 <script type="text/javascript" >
     7     var json=[
     8     {
     9         "empno": 7369,
    10         "ename": "李宁",
    11         "job": "软件工程师",
    12         "hiredate": "2017-05-12",
    13         "salary": 13000,
    14         "dname": "研发部"
    15     },
    16     {
    17         "empno": 7499,
    18         "ename": "王乐",
    19         "job": "客户经理",
    20         "hiredate": "2017-04-22",
    21         "salary": 10000,
    22         "dname": "市场部",
    23         "customers": [
    24             {
    25                 "cname": "小王"
    26             },
    27             {
    28                 "cname": "小罗"
    29             }
    30         ]
    31     }
    32 ];
    33     console.log(json);
    34     for(var i=0;i<json.length;i++){
    35         var emp=json[i];
    36         document.write("<h1>");
    37         document.write(emp.empno);
    38         document.write(","+emp.ename);
    39         document.write(","+emp.job);
    40         document.write(","+emp.hiredate);
    41         document.write(","+emp.salary);
    42         document.write(","+emp.dname);
    43         document.write("</h1>");
    44         
    45         if(emp.customers!=null){
    46             document.write("<h2>---");
    47             for(var j=0;j<emp.customers.length;j++){
    48                 var customer=emp.customers[j];
    49                 document.write(customer.cname+" ");
    50             }
    51             document.write("</h2>");
    52         }
    53     }
    54 </script>
    55 </head>
    56 <body>
    57     
    58 </body>
    59 </html>

     2.JSON与字符串转换

     1 <title>字符串转JSON</title>
     2 <script type="text/javascript">
     3     var str="{"class_name":"五年级三班"}";//str.name
     4     var json=JSON.parse(str);
     5     console.log(str);
     6     console.log(json);
     7     document.write("班级:"+json.class_name);
     8 </script>
     9 
    10 <title>JSON转字符串</title>
    11 <script type="text/javascript">
    12     var json={"class_name":"五年级四班"};
    13     var str1=JSON.stringify(json);
    14     console.info(str1);
    15     console.info(json);
    16     console.log(str1);
    17     console.log(json);
    18     var json1={};
    19     json1.name="哈哈哈"
    20     console.info(json1);
    21 
    22 </script>

    3.JSON与Java交互

     

     1 package json;
     2 
     3 import java.util.Date;
     4 
     5 import com.alibaba.fastjson.annotation.JSONField;
     6 
     7 import sun.java2d.d3d.D3DScreenUpdateManager;
     8 
     9 public class Employee {
    10     private Integer empno;
    11     private String ename;
    12     private String job;
    13     //@JSONField(format = "yyyy-MM-dd HH:mm:ss SSS")
    14     @JSONField(name="hiredate" ,format = "yyyy-MM-dd" )
    15     private Date hdate;
    16     private Float salary;
    17     @JSONField(serialize=false )
    18     private String dname;
    19     
    20     public Employee() {
    21         super();
    22     }
    23     public Employee(Integer empno, String ename, String job, Float salary,String dname,  Date hdate) {
    24         super();
    25         this.empno = empno;
    26         this.ename = ename;
    27         this.job = job;
    28         this.hdate = hdate;
    29         this.salary = salary;
    30         this.dname = dname;
    31     }
    32     
    33     public Integer getEmpno() {
    34         return empno;
    35     }
    36     public void setEmpno(Integer empno) {
    37         this.empno = empno;
    38     }
    39     public String getEname() {
    40         return ename;
    41     }
    42     public void setEname(String ename) {
    43         this.ename = ename;
    44     }
    45     public String getJob() {
    46         return job;
    47     }
    48     public void setJob(String job) {
    49         this.job = job;
    50     }
    51     public Date getHdate() {
    52         return hdate;
    53     }
    54     public void setHdate(Date hdate) {
    55         this.hdate = hdate;
    56     }
    57     public Float getSalary() {
    58         return salary;
    59     }
    60     public void setSalary(Float salary) {
    61         this.salary = salary;
    62     }
    63     public String getDname() {
    64         return dname;
    65     }
    66     public void setDname(String dname) {
    67         this.dname = dname;
    68     }
    69     
    70 }
     1 package json;
     2 
     3 import java.util.*;
     4 
     5 import com.alibaba.fastjson.JSON;
     6 
     7 public class FastJsonSample1 {
     8     public static void main(String[] args) {
     9         Date date = new Date();
    10         Calendar c=Calendar.getInstance();
    11         c.set(2020, 1, 30, 0, 0, 0);
    12         Employee employee=new Employee(4488,"奇锋","前端工程师",1000f,"市场部",c.getTime());
    13         
    14         //序列化:FastJSON中提供了JSON对象,完成对象与JSON字符串的互相转换
    15         String json=JSON.toJSONString(employee);
    16         System.out.println(json);
    17         
    18         //反序列化:FastJSON,json转其他对象  
    19         Employee emp=JSON.parseObject(json,Employee.class);
    20         System.out.println(emp.getEname());
    21     }
    22 }
    package json;
    
    import java.util.*;
    
    import com.alibaba.fastjson.JSON;
    import com.sun.org.apache.bcel.internal.generic.NEW;
    
    import sun.management.counter.Variability;
    
    public class FastJsonSample2{
        public static void main(String[] args) {
            List emplist=new ArrayList();
            for(int i=1;i<=100;i++) {
                Employee employee=new Employee();
                employee.setEmpno(4488+i);
                employee.setEname("员工"+i);
                emplist.add(employee);
            }
            
            //序列化:FastJSON中提供了JSON对象,完成对象与JSON字符串的互相转换
            String json=JSON.toJSONString(emplist);
            System.out.println(json);
    
    
            //反序列化:FastJSON,json转其他对象  
            List<Employee> emps=JSON.parseArray(json,Employee.class);
            for(Employee e:emps) {
                System.out.println(e.getEmpno()+":"+e.getEname());
            }
    
        }
    }
  • 相关阅读:
    那些离不开的 Chrome 扩展插件
    Spring Boot 实战 —— 入门
    Maven 学习笔记
    Linux lvm 分区知识笔记
    Linux 双向 SSH 免密登录
    CentOS Yum 源搭建
    Ubuntu 系统学习
    iOS 测试三方 KIF 的那些事
    Swift 网络请求数据与解析
    iOS Plist 文件的 增 删 改
  • 原文地址:https://www.cnblogs.com/qqfff/p/12301945.html
Copyright © 2011-2022 走看看