[Python] json format conversion② ( Introduction to Json format | Json concept | Json function | Object/array format | Nested format | Json features )





1. Introduction to Json format




1. The concept of JSON


The English full name of Json is "JavaScript Object Notation", JavaScript Object Notation;

Json is a lightweight data exchange format;

Basic elements in Json are strings, numbers, boolean values ​​or null,

  • The key-value pairs in the Json object can be elements of the above types;
  • The elements in the Json array can be elements of the above types;

2. Json function


The main function of Json is data transfer and interaction in different programming languages;


For example: Python transfers data to Java, and directly transfers the container variable in Python. Java must not be able to parse the value of the variable. After converting the data in Python into a Python string, and then passing it to Java, the data interaction between Python and Java can be realized;

Similarly, when Java transfers data to Python, the Java data can be converted into a Json string, and then passed to the Python language;


3. Json format - object/array format


The basic format of Json mainly has two forms: object and array.

  • Json object format: A Json object is a key-value pair enclosed in curly braces, the key and value are separated by a colon, and each key-value pair is separated by a comma;
{
    
      
  "name": "John",  
  "age": 30,  
  "city": "New York"  
}
  • Json array format: Json arrays are stored in square brackets, and each array element is separated by commas;
[  
  "apple",  
  "banana",  
  "orange"  
]

The Json object corresponds to the dictionary in Python, and the Json array corresponds to the list in Python. The above correspondence can be seamlessly converted;


4. Json format - object/array nested format


The keys and values ​​in the Json object can be objects or arrays;

The elements in the Json array can be objects or arrays;


The following Json data is a Json object,

  • The value corresponding to the "hobbies" key is an array, and the elements of the array are strings;
  • The value corresponding to the "address" key is an object, and the object is a key-value pair;
{
    
      
  "name": "John",  
  "hobbies": [  
    "reading",  
    "traveling"  
  ],  
  "address": {
    
      
    "street": "123 Main St",  
    "city": "Anytown",  
    "state": "CA",  
    "zip": "12345"  
  }  
}

5. Json features


Json can perform data exchange and communication between different platforms and programming languages, and has the following characteristics:

  • Simple and easy to read: JSON format is simple, easy to read and write, and easy to parse and generate by machine;
  • Cross-platform compatibility: JSON can exchange data between different operating systems, programming languages ​​and platforms, and has good cross-platform compatibility;
  • Does not depend on a specific programming language: JSON is a programming language-independent format that enables data exchange and communication between different programming languages;
  • Easy to handle: JSON data can be directly transmitted in HTTP requests and responses, enabling data exchange and communication in web applications;

Guess you like

Origin blog.csdn.net/han1202012/article/details/131863620