Java EE Development Technology Course Week 7 (json)

JSON:

https://baike.baidu.com/item/JSON/2462549?fr=aladdin

JSON stands for JavaScript Object Notation, which is a lightweight data exchange format. It uses a text format that is completely independent of programming languages ​​to store and represent data, effectively improving network transmission efficiency.

JSON syntax

JSON syntax rules
In the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, etc. But objects and arrays are two special and commonly used types:
  • Objects are represented as key-value pairs
  • Data is separated by commas
  • Curly braces hold the object
  • Square brackets hold arrays
 

JSON key/value pair

JSON key-value pair is a way to save JS objects, and it is similar to the way of writing JS objects. The key name in the key/value pair combination is written in front and wrapped in double quotes "", separated by colon :, and then followed by value:
1
{"firstName": "Json"}
This is easy to understand and is equivalent to this JavaScript statement:
1
{firstName : "Json"}
 

The relationship between JSON and JS objects

Many people don't understand the relationship between JSON and Js objects, and they don't even know who is who. In fact, it can be understood as follows:
JSON is a string representation of JS objects, which uses text to represent the information of a JS object, which is essentially a string.
Such as
1 var obj = {a: 'Hello', b: 'World'}; // This is an object, note that the key name can also be wrapped in quotes 2 var json = '{"a": "Hello", "b ": "World"}'; // This is a JSON string, essentially a string

Convert JSON and JS objects

To convert from an object to a JSON string, use the JSON.stringify() method:
1 var json = JSON.stringify({a: 'Hello', b: 'World'}); //结果是 '{"a": "Hello", "b": "World"}'

To convert from JSON to object, use the JSON.parse() method:

var obj = JSON.parse('{"a": "Hello", "b": "World"}'); //结果是 {a: 'Hello', b: 'World'}

Common type

edit
In the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, etc. But objects and arrays are two special and commonly used types.
Object: Object is the content enclosed by {} in curly braces in JS, and the data structure is a key-value pair structure of {key1: value1, key2: value2, ...}. In object-oriented languages, keys are properties of objects and values ​​are corresponding values. Key names can be represented using integers and strings. The type of the value can be any type.
Array: Array is the content enclosed in square brackets [] in JS, and the data structure is the index structure of ["java", "javascript", "vb", ...]. In JS, arrays are a special data type that can also use key-value pairs like objects, but indexes are used more often. Likewise, the type of the value can be any type.
 

Basic example

edit
simply  [1], JSON can convert a set of data represented in a JavaScript object into a string, which can then be easily passed between networks or programs and restored to a data format supported by each programming language when needed , for example in PHP, you can restore JSON to an array or a primitive object. When using AJAX, if you need to use an array to pass values, then you need to use JSON to convert the array to a string.
 

representation object

The most common format of JSON is object key-value pairs. For example the following:
{"firstName": "Brett", "lastName": "McLaughlin"}

represents an array

Like normal JS arrays, JSON represents arrays using square brackets [].
copy code
{
     
 
"people":[
         
 
{
 
"firstName": "Brett",
             
"lastName":"McLaughlin"
          },
         
  {
              "firstName":"Jason",
  "lastName":"Hunter" } ] }
copy code
This is not difficult to understand. In this example, there is only one variable named people, and the value is an array with two entries, each entry is a record of a person, which contains first and last name. The above example shows how to use parentheses to combine records into a single value. Of course, many more values ​​(each containing multiple records) can be represented using the same syntax.
There are no predefined constraints to obey when working with data in JSON format. Therefore, in the same data structure, the way of representing data can be changed, and the same thing can be represented in different ways.
As mentioned earlier, in addition to objects and arrays, you can also simply use strings or numbers to store simple data, but it doesn't make much sense.
 
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324775439&siteId=291194637