JSON function
Using the JSON function requires importing the json library: import json.
Function Description
json.dumps Encode Python objects into JSON strings
json.loads Decode encoded JSON strings into Python objects
json.dumps
Syntax
json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None , encoding="utf-8", default=None, sort_keys=False, **kw)
Example
The following example encodes the array into JSON format data:
#!/usr/bin/python import json data = {'number': 6, 'name': 'Pythontab'} jsonData = json.dumps(data) print jsonData
The execution result of the above code is:
{"number": 6, "name": "Pythontab"}
Note: You may find that after executing the above conversion, the data has not changed. Here is what I want to say: In json, double quotes are the marked strings. Separation symbols, single quotes are not standard.
Use parameters to sort JSON data and format the output:
#!/usr/bin/python import json data = {'number': 6, 'name': 'Pythontab'} jsonData = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')) print jsonData
Output results
{ "name": "Pythontab", "number": 6 }
Conversion table from python original type to json type:
Python | JSON |
---|---|
object | |
array | |
string | |
number | |
true | |
false | |
null |
json.loads is used to decode JSON data. This function returns the data type of the Python field.
Syntax
json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]] ]])
Examples
The following examples show how Python decodes JSON objects:
#!/usr/bin/python import json jsonData = '{"number": 6, "name": "Pythontab"}' str = json.loads(jsonData) print str
The execution result of the above code is:
{u'number': 6, u'name': u'Pythontab'}
json type conversion to Python type comparison table:
array | |
string | |
number (int) | |
number (real) | |
true | |
##false | |
null | |
Github address: https://github.com/dmeranda/demjson
Environment configuration
Before using Demjson to encode or decode JSON data, we need to install the Demjson module first .
Method 1: Source code installation
$ tar -xvzf demjson-2.2.4.tar.gz
$ cd demjson-2.2.4
$ python setup.py install
Method 2: Use pip to install directly
pip install Demjson
JSON function
Function Description
encode Encode Python objects into JSON strings
decode You can use the demjson.decode() function to decode JSON data. This function returns the data type of the Python field.
encode syntax
demjson.encode(self, obj, nest_level=0)
decode syntax
demjson.decode(self, txt)
It’s very simple to use, so I won’t give examples here~~
The above is the detailed content of Detailed explanation of Python's parsing of JSON. For more information, please follow other related articles on the PHP Chinese website!

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
