Learn JSON

Learn JSON

Introducing JSON

The JSON: the JavaScript Object Notation (the JavaScript Object Notation) lightweight text data interchange format.

JSON syntax rules : JSON an object is stored in the form of key-value pairs { "key": "value"} represents an array manner as [{target} JSON, JSON objects {}]

Example : Object {"ename":"Jack","age":"25"}

Array [{"ename":"Jack","age":"25"},{"ename":"Lucy","age":"24"}]

JSON and JavaScript interaction

  • Converts a string to a JSON object JSON.parse()

demo1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串转JSON</title>
    <script type="text/javascript">
        var str = "{\"name\":\"张三\"}";
        var json = JSON.parse(str);
        console.log(str);
        console.log(json);
        document.write("姓名:" + json.name);//姓名:张三
    </script>
</head>
<body>
</body>
</html>
  • Converting a string JSON object JSON.stringify()

demo2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JSON转字符串</title>
    <script type="text/javascript">
        var json1 = {"name" : "张三"};
        var str= JSON.stringify(json1);
       document.write(str);//{"name":"张三"}
    </script>
</head>
<body>

</body>
</html>

JSON interact with Java

FastJSON parser

FastJSON jar package download: https://repo1.maven.org/maven2/com/alibaba/fastjson/1.2.53/fastjson-1.2.53.jar

Student.java

package JSON;

public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

FastJSON serialization

The JavaBean object into a JSON-formatted string JSON.toJSONString(Object)

JSONdemo1.java

package JSON;

import com.alibaba.fastjson.JSON;
public class JSONdemo1 {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("张三");
        stu.setAge(20);
        String s = JSON.toJSONString(stu);
        System.out.println(s);//{"age":20,"name":"张三"}
    }
}

FastJSON deserialization

The JSON-formatted string into a Java Bean objects  JSON.parseArray(list, Object.class)

JSONdemo2.java

package JSON;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;

public class JSONdemo2 {
    public static void main(String[] args) {
        List list=new ArrayList();
        list.add(new Student("张三",18));
        list.add(new Student("李四",19));
        String s = JSON.toJSONString(list);
        System.out.println(s);//[{"age":18,"name":"张三"},{"age":19,"name":"李四"}]
        List<Student> stu = JSON.parseArray(s, Student.class);
        for (Student student : stu) {
            System.out.println(student.getName()+":"+student.getAge());
        }//张三:18  李四:19
    }
}

FastJSON comment

@JSONField(serialize="true/false") Serialize set value set whether true or false to the following properties of the sequence

@JSONField(format="0,000.00") Formatting attribute format

Guess you like

Origin www.cnblogs.com/jascen/p/11256575.html