Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of json Operation examples in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "json operation case analysis in Python", the content is detailed, the steps are clear, and the details are handled properly. I hope that this "json operation case analysis in Python" article can help you solve your doubts.

1. What is Json?

JSON (JavaScript Object Notation) is a lightweight data interchange format that is a subset of JavaScript and is easy for people to read and write.

The data exchange between the front end and the back end is actually the data exchange between JS and Python.

JSON considerations:

(1) the name must be included in double quotes (that is, "")

(2) the value can be a string, number, true, false, null, JavaScript array, or sub-object enclosed in double quotes.

Mapping relationship between 2.python data type and json data type

Python

JSONdictobjectlist, tuplearraystr, unicodestringint, long, floatnumberTruetrueFalse

False

Nonenull3. Methods commonly used in json

Before using the json module, first import the json library: import json

Method description json.dumps () encodes the Python object into a JSON string json.loads () decodes the encoded JSON string into a Python object json.dump () serializes the Python built-in type into a json object and then writes to the file json.load () reads the string elements in the file in the form of json and converts them into Python types

For example:

3-1 json.dumps () import json data = {'name':'nanbei','age':18} # encodes the Python object into the json string print (json.dumps (data))

Results:

{"name": "nanbei", "age": 18}

Note: we can see here that the original single quotation marks have become double quotation marks.

3-2 json.loads () import json data = {'name':'nanbei','age':18} # Encoding Python objects into json strings # print (json.dumps (data)) # Encoding json strings into Python objects a = json.dumps (data) print (json.loads (a))

Results:

{'name':' nanbei', 'age': 18}

Here are examples of tuples and lists:

Import json data = (1Jing 2Jing 3Jing 4) data_json = [1Jing 2Jing 3Jing 4] # Encoding Python objects into json strings print (json.dumps (data)) print (json.dumps (data_json)) # Encoding json strings into Python objects a = json.dumps (data) b = json.dumps (data_json) print (json.loads (a) print (json.loads (b))

Results:

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

As you can see, both tuples and lists parse out arrays.

3-3 json.dump () import json data = {'nanbei':'', 'aqiao: [1 nanbei':'', 2 as 3],' baked: (1 json_test.txt','w+' 2) as f: json.dump (data,f)

View the results:

3-4 json.load () import json data = {'nanbei':'', 'aqiao: [1print 2je 3je 4],' baked: (1Zhong 2pas 3)} with open ('json_test.txt','w+') as f: json.dump (data,f) with open (' json_test.txt','r+') as f: print (json.load (f))

Results:

{'a ': [1, 2, 3, 4], 'baked: [1, 2, 3],' nanbei':'}

4. Detailed description of parameters:

Dump (obj,skipkeys=False, ensure_ascii=True, check_circular=True

Allow_nan=True, cls=None, indent=None, separators=None

Default=None, sort_keys=False, * * kw):

# function: convert Python objects into JSON objects

# skipkeys: if it is True, it can only be a dictionary object, otherwise it will TypeError error. Default is False.

# ensure_ascii: determine whether to encode ASCII

# check_circular: loop type check, if True

# allow_nan: determine whether it is an allowed value

# indent: print, render, and indent in a beautiful way

# separators: object delimiter. Default is

# encoding: encoding method. Default is utf-8.

# sort_keys: if you select True for a dictionary object, it will be sorted by the ASCII code of the key

For dump, it's just an extra fp parameter.

Dump (obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True

Allow_nan=True, cls=None, indent=None, separators=None

Default=None, sort_keys=False, * * kw)

Serialize ``obj`` as a JSON formatted stream to ``fp`` (a

``. Write ()``-supporting file-like object).

To put it simply, dump needs a parameter similar to a file pointer (not a real pointer, which can be called a file object), combined with file operations, that is, first convert Python file objects into json strings and then save them in the file.

After reading this, the article "Analysis of json Operation examples in Python" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report