Usage scenarios and precautions of JSON


In the last part, we explained the birth of JSON because XML is integrated into HTML, and the implementation details of each browser are differentDouglas Crockford(Douglas Crockford) andChip Morningstar (chip Morningstar) together extracted a subset from JS data type as a new data exchange format. Because mainstream browsers use common JavaScript engine components, there is no compatibility problem when parsing this new data format. Therefore, they named this data format “JavaScript object notation”, abbreviated as JSON, so JSON was born!

Today, let’s learn about the structure, data types, usage scenarios and precautions of JSON!

1、 JSON format

As we know above, JSON is a subset extracted from JavaScript data types. How many structural forms does JSON have? What are the data types? What data types do they correspond to in JavaScript?

1. Two structural forms of JSON

1. Key value pair form

In the last issue, we gave an example of JSON in the form of key value pairs, as follows:

  "person": {
    "name": "pig",
    "age": "18",
    "sex": "man",
    "hometown": {
      "Province": Jiangxi Province,
      "City": Fuzhou City,
      "County": Chongren County

The JSON data rules of this structure are as follows:An unordered collection of name / value pairs. An object begins with the {left bracket and ends with the} right bracket. Each “name” is followed by a colon; “name / value” pairs are separated by commas,。
Usage scenarios and precautions of JSON
2. Array form
Because most of the time, the JSON you use may be in the above key value form, so many people will always ignore the array form when explaining JSON, which needs to be noted.

What is the array form of JSON? Pig brother also give an example!

["pig", 18, "man", "Chongren County, Fuzhou City, Jiangxi Province"]

JSON data in array formIt’s an ordered set of values. An array begins with the left bracket and ends with the right bracket. Values are separated by commas.
Usage scenarios and precautions of JSON

2. Six data types of Josn

The above two JSON forms contain value internally. What are the types of value in JSON? What are the types of values in JSON? Last time, we said that JSON actually extracts a subset from JS data format. What are the specific data types?

  1. String: string, which must be enclosed in double quotation marks.
  2. Number: numeric value, which is consistent with JavaScript number, and integer (not using decimal point or exponential counting method) is at most15Bit. The maximum number of decimal places is17
  3. Object: the object form of JavaScript{ key:value }Can be nested.
  4. Array: array, array representation of JavaScript [value], can be nested.
  5. True / false: Boolean type, boolean type of JavaScript.
  6. Null: null value, null of JavaScript.

Usage scenarios and precautions of JSON
The above data format pictures are from the official JSON document:

2、 JSON usage scenarios

After introducing the data format of JSON, let’s take a look at the scenarios in which JSON is widely used in enterprises.

1. The interface returns data

The most common use of JSON is web. The current data interfaces are basically returned JSON. The detailed scenarios are as follows:

  1. Ajxa access data asynchronously
  2. RPC remote call
  3. The front and back end separate the data returned by the back end
  4. Open API, such as Baidu, Gaode and other open interfaces
  5. Inter enterprise cooperation interface

This API interface generally provides an interface document, which describes the input and output parameters of the interface,
Usage scenarios and precautions of JSON
General interface return data will be encapsulated in JSON format, such as the following

    "code": 1,
    "msg": "success",
    "data": {
        "name": "pig",
        "age": "18",
        "sex": "man",
        "hometown": {
            "Province": Jiangxi Province,
            "City": Fuzhou City,
            "County": Chongren County

2. Serialization

When the program is running, all variables are stored in memory. If the program is restarted or the machine is down, the data will be lost. In general, runtime variables are not so important, but some data in memory need to be saved for the next program or other programs.

Save the data in memory, either in the database or directly to the file, andThe process of converting data in memory into data that can be saved or transferred is called serializationIt is called pickling in Python and serialization, marshalling, flatting and so on in other languages.

The normal serialization is to convert theThe object is converted directly to be saveable or transportable, which saves the type information of the object, while JSON serialization does not preserve the object type!

In order to let everyone feel the difference more intuitively, pig brother uses the code to do a test, we can see at a glance
Usage scenarios and precautions of JSON

  1. Direct serialization of Python objects will save class information, and will become Python objects the next time you load them into memory.
  2. JSON object serialization only saves attribute data and does not retain class information. Next time loads are used to load into memory, it can be directly converted into dict object or person object, but auxiliary methods need to be written.

As for the feature that JSON serialization cannot save class information, what is the use of JSON serialization? The answer is, of course, useful for serialization reading in different programming languages, such as:I use Python to crawl data and convert it into an object. Now I need to serialize it to disk, and then use java language to read this data. At this time, due to different cross language data types, JSON serialization is needed.

Existence is reasonable, two serialization can be selected according to the demand!

3. Generate token

First of all, there are various forms of token, such as JSON, string, number and so on. As long as it can meet the requirements, it is not specified which form to use.

The most representative token in JSON format is JWT (JSON web tokens).
Usage scenarios and precautions of JSON
With the development of technology and the popularity of distributed web applications, the cost of managing the user’s login status through session is getting higher and higher. Therefore, it has gradually developed into a token method to do login identity verification, and then retrieve the cached user information in redis through token. With the advent of JWT, the verification method is more simple and convenient, and it does not need to be cached by redis, but directly retrieved according to the token Save the user information, as well as the token availability verification, single sign on is more simple.

Zhuge also used JWT to log in to the app. The general process is as follows:

  1. The user enters the user name and password
  2. App requests login center to verify user name and password
  3. If the verification is passed, a token is generated, in which the token contains the user’s uid, token expiration time, expiration delay time, etc., and then it is returned to the app
  4. The token is obtained by the app and saved in the cookie. It is brought with the next request for other services
  5. Other services are called to login center interface verification after getting to Token.
  6. Response if verification passes

What are the advantages of JWT login authentication

  1. Good performance: the server does not need to save a large number of sessions
  2. Single sign on (log in to an application, and other applications in the same enterprise can access it): using JWT to do a login center is basically completed and easy to implement.
  3. Good compatibility: it supports mobile devices and cross program calls. Cookie is not allowed to cross domain access, but token does not have this problem.
  4. Good security: because of the signature, JWT can prevent tampering.

More JWT related knowledge on-line learning, but this article introduces more!

4. Configuration file

To be honest, there are not many scenarios in which JSON is used as a configuration file. The most representative one is NPM package.json Package management configuration file, the following is a NPM package.json Configuration file content.

  "Name": "server", // project name
  "version": "0.0.0",
  "private": true,
  "main": " server.js ", // project entry address, that is, the project to be executed after NPM is executed
  "scripts": {
    "Start": "node. / bin / www" // scripts specifies the NPM command line abbreviation for running script commands
  "dependencies": {
    "Cookie parser": "~ 1.4.3", // specifies the modules required for project development
    "debug": "~2.6.9",
    "express": "~4.16.0",
    "http-errors": "~1.6.2",
    "jade": "~1.11.0",
    "morgan": "~1.9.0"

But in factJSON is not suitable for configuration file because it can’t write comments and has poor readability as a configuration file.

There are many formats of configuration files, such as toml, yaml, XML, ini and so on. At present, yaml is used as configuration file in many places.

3、 The use of JSON in Python

Finally, let’s take a look at the ways to operate JSON in Python, and we need to introduce them when we operate JSON in PythonjsonStandard library.

import json

Type conversion 1

1. Python type to JSON: json.dump ()

#1. Convert the dict type of Python to JSON
person_ Dict = {'name':'pig ','age': 18, 'sex':'Man ','hometown':'fuzhou, Jiangxi Province '}
#The indent parameter is the number of indented spaces
person_dict_json = json.dumps(person_dict, indent=4)
print(person_dict_json, '\n')

#2. Convert Python list type to JSON
person_ List = ['pig ', 18,' man ','fuzhou, Jiangxi']
person_list_json = json.dumps(person_list)
print(person_list_json, '\n')

#3. Convert Python object type to JSON
person_ Obj = person ('pig ', 18,' man ','fuzhou, Jiangxi')
#The middle anonymous function is a dictionary form that gets all the properties of an object
person_obj_json = json.dumps(person_obj, default=lambda obj: obj.__dict__, indent=4)
print(person_obj_json, '\n')

Implementation results:
Usage scenarios and precautions of JSON

2. JSON to Python type: json.loads ()

#4. Dict type of JSON to Python
person_ JSON = '{name': "pig", "age": 18, "sex": "man", "Hometown": "Fuzhou, Jiangxi"} '
person_json_dict = json.loads(person_json)
print(type(person_json_dict), '\n')

#5. List type of JSON to Python
person_ Json2 = '["pig", 18, "man", "Fuzhou, Jiangxi"]'
person_json_list = json.loads(person_json2)
print(type(person_json_list), '\n')

#6. Custom object type of JSON to Python
person_ JSON = '{name': "pig", "age": 18, "sex": "man", "Hometown": "Fuzhou, Jiangxi"} '
# object_ The hook parameter is to convert the dict object into a custom object
person_json_obj = json.loads(person_json, object_hook=lambda d: Person(d['name'], d['age'], d['sex'], d['hometown']))
print(type(person_json_obj), '\n')

The results are as follows:
Usage scenarios and precautions of JSON

2. Corresponding data type

We have demonstrated the conversion between Python types and JSON. At the beginning, we said that JSON has six data types. Which data types do these six data types correspond to in Python?
Usage scenarios and precautions of JSON

3. Points needing attention

  1. The key name and string of JSON must be enclosed by double quotation marks, and the single quotation mark in Python can also be expressed as a string, so this is a relatively easy error to make!
  2. When Python type and JSON are converted to each other, are they usedload/dumpOr use itloads\dumps? What’s the difference between them? When should I add itsWhen not to adds? We can find the answer by looking at the source code: NosThere is one more parameter in the method offpIndicates the filepath. Finally, there is an operation to write to the file. So when we remember, we can remember it like this:plussRepresents the conversion to a string (STR), without addingsIt means to convert it into a file.Usage scenarios and precautions of JSON
  3. When Python custom objects are converted to JSON, auxiliary methods are needed to indicate the corresponding relationship between attributes and key names. If a method is not specified, an exception will be thrown!

Usage scenarios and precautions of JSON

  1. I believe that some students who look carefully will be curious about the use of pig json.dumps Method to convert Python type to JSON, if Chinese appears, the following will appear:u6c5fu897fu629au5ddeThis kind of thing, why? as a result of:When doing dumps operation, JSON in Python 3 will convert Chinese into Unicode code and store it in hexadecimal mode, not UTF-8!

4、 Summary

Today, we have learned two forms of JSON. Remember that JSON also has[...]In this form.

I learned about the six data types of JSON, and which types they used in Python.

Learn about some usage scenarios of JSON and practical examples.

You also learned how to use JSON in Python and what to note.

Combined with the introduction of the birth and development of JSON in the previous issue, we basically introduced the knowledge related to JSON. Later, we will give some actual cases of crawlers to teach you how to parse and return JSON data.

A JSON knowledge point is divided into two long articles (nearly 10000 words), and its importance is self-evident. Because whether you are doing crawler, data analysis, web, or even front-end, testing, operation and maintenance, JSON is a knowledge point you must master!
Usage scenarios and precautions of JSON

Recommended Today

ASP.NET Example of core MVC getting the parameters of the request

preface An HTTP request is a standard IO operation. The request is I, which is the input; the responsive o is the output. Any web development framework is actually doing these two things Accept the request and parse to get the parameters Render according to the parameters and output the response content So let’s learn […]