[Python module] Detailed explanation of python parsing json files

1. What is a json file

JSON (Java Script Object Notation) is a format commonly used to transfer data (mainly via APIs) in a way that does not "burden the system". The basic principle is to use text to record data points and to transmit data points to third parties.

JSON is a format for storing data objects using text. In other words, it is a data structure that represents objects in textual form . Despite its origins in JavaScript, it has become the de facto standard for transferring objects.
Most popular programming languages ​​support the JSON format, including Python. Files in JSON format are often used by APIs to transfer data objects. The following is an example of a JSON string:

{
    
    
   "name": "United States",
   "population": 331002651,
   "capital": "Washington D.C.",
   "languages": [
  "English",
  "Spanish"
   ]
}

In this example, the JSON data looks like a Python dictionary. Like a dictionary, JSON passes data in the form of key-value pairs. However, JSON data can also be strings, numbers, booleans or lists.

XML has been a common choice for representing data objects in text format until JSON became popular. Here is an example of the same information in XML format:

<?xml version="1.0" encoding="UTF-8"?>
<country>
   <name>United States</name>
   <population>331002651</population>
   <capital>Washington D.C.</capital>
   <languages>
       <language>English</language>
       <language>Spanish</language>
   </languages>
</country>

Obviously, JSON code is less. This is one of the main reasons JSON is so popular.

JSON is constructed from two structures:
json is an object in Javascript and an object in an array. Essentially, it is a string with a specific structure , so various complex structures can be expressed through these two structures:

  • 1 Object: The object is expressed as the content enclosed by "{}" in js, and the data structure is the key-value pair structure of {key:value, key:value...}. In the object-oriented language, the key is the attribute of the object. value is the value of the corresponding attribute, so it is easy to understand. The attribute value value can be: number, string, array, object, etc.
  • 2 Array: In js, an array is the content enclosed by square brackets "[]", and the data structure is ["java", "javascript", "vb"….] The value method is the same as that of all languages, using the index to obtain, the field The type of value is: number, string, array, object, etc.

Simply put, JSON converts a set of data represented in a JavaScript object into a string, which can then be passed easily between functions, or from a web client to a server in an asynchronous application terminal program. This string looks a bit odd, but JavaScript interprets it easily, and JSON can represent more complex structures than "name/value pairs". For example, arrays and complex objects can be represented, not just simple lists of keys and values.

If you don't know JSON yet, you can read the JSON tutorial first .

2. Commonly used interfaces

In Python3, you can use the json module to encode and decode JSON data, which mainly includes the following four operation functions:
insert image description here
Tip: The so-called file-like objects refer to those objects with read() or write() methods, for example, f = open(' a.txt','r'), where f has a read() method, so f is a file-like object.

Python's JSON module has four main functions: read(), reads(), load(), and loads(). These features are often confusing. The most important place is the letter "s" stands for String. In addition, the letter "s" in the functions loads() and dumps() needs to be read separately, that is, loads reads load-s, and dumps() reads dump-s.

Here's a quick table to help you remember these functions:
insert image description here

  • Generally, loads is used to read JSON strings, and load() is used to read JSON data in files.
    The load() method takes a file object and returns JSON data parsed as a Python object.
  • The method used to write JSON files is dump(). This method is very similar to the dumps() method. The only difference is that dumps() returns a string and dump() writes to a file.

type conversion

During the encoding and decoding process of json, the original type of python and the JSON type will be converted to each other. The specific conversion comparison is as follows:

  • Python encoding to JSON type conversion correspondence table:
    insert image description here
  • JSON decoding to Python type conversion correspondence table:
    insert image description here

3. Code example

For details, please read the blog

import json
 
data = {
    
    
    'name': 'pengjunlee',
    'age': 32,
    'vip': True,
    'address': {
    
    'province': 'GuangDong', 'city': 'ShenZhen'}
}
# 1. 将 Python字典类型 转换为 JSON对象
json_str = json.dumps(data)
print(json_str) # 结果 {"name": "pengjunlee", "age": 32, "vip": true, "address": {"province": "GuangDong", "city": "ShenZhen"}}
 
# 2. 将 JSON对象类型 转换为 Python字典
user_dic = json.loads(json_str)
print(user_dic['address']) # 结果 {'province': 'GuangDong', 'city': 'ShenZhen'}
print(user_dic.keys())
 
# 3. 将 Python字典 直接输出到 文件
with open('pengjunlee.json', 'w', encoding='utf-8') as f:
    json.dump(user_dic, f, ensure_ascii=False, indent=4) # 缩进4个空格
 
# 4. 将 类文件对象中的JSON字符串 直接转换成 Python字典
with open('pengjunlee.json', 'r', encoding='utf-8') as f:
    ret_dic = json.load(f)
    print(type(ret_dic)) # 结果 <class 'dict'>
    print(ret_dic['name']) # 结果 pengjunlee
  • Simple conversion between strings and Python types can be achieved using eval().
user1 = eval('{"name":"pengjunlee"}')
print(user1['name']) # 结果 pengjunlee

Guess you like

Origin blog.csdn.net/All_In_gzx_cc/article/details/125717758