Home  >  Article  >  Backend Development  >  What are the Python json types? List examples to parse Python json

What are the Python json types? List examples to parse Python json

Tomorin
TomorinOriginal
2018-08-17 14:44:002772browse

There is json in any language, In this chapter we will use examples to analyze and introduce how to use Python language to code, and how to use Python languageGo to decode the JSON object.

JSON(JavaScript Object Notation) is a lightweight data exchange format that is easy for humans to read and write.

JSON function

Using JSON functionneeds to import the json library: import json.

What are the Python json types? List examples to parse Python json

json.dumps

##json.dumps is used to encode Python objects into JSON strings.

Grammar

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)

Examples

The following examples Encode the array into JSON format data:

#!/usr/bin/python
import json

data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]

json = json.dumps(data)
print json

The execution result of the above code is:

[{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]

Use parameters to format the JSON data for output:

>>> import json
>>> print json.dumps({'a': 'Runoob', 'b': 7}, sort_keys=True, indent=4, separators=(',', ': '))
{
    "a": "Runoob",
    "b": 7
 }

python original type Conversion table to json type:

What are the Python json types? List examples to parse Python json


The above is the detailed content of What are the Python json types? List examples to parse Python json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn