PythonPython3java
 

Python JSON

In this chapter we will show you how to use the Python language to encode and decode JSON objects.

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


JSON function

Using the JSON function requires importing the json library: import json.

FunctionDescription
json.dumps Encode Python objects into JSON strings
json.loadsDecode the encoded JSON string into a Python object

json.dumps

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

Syntax

json.dumps(obj, skipkeys =False, ensure_ascii=True, check_circular=True, allow_nan=True,< Span class="pln"> cls=None, indent=None,< /span> separators=None< Span class="pun">, encoding="utf-8 ", default=None, sort_keys=False, **kw)

Example

The following example encodes an array into JSON formatted data:

#!/usr/bin/python
import json

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

json = json.dumps(data)
    
    
#!/usr/bin/python
import json

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

json = json.dumps(data)
print json

The above code execution result is:

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

Use parameters to format JSON data output:

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

The conversion table for python primitive types to json types:

Python JSON
dict object
list, tuple array
str, unicode string
int, long, float number
True true
False false
None null

json.loads

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]]]]]]]])

Example

The following example shows how Python decodes a JSON object:

#!/usr/bin/python
import json

jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

text = json.loads(jsonData)
print text

The above code execution result is:

{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}

json Type conversion table to type conversion to python...:

JSON Python
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None
demjson.encode(self, obj, nest_level=0)

Example

The following example encodes an array into JSON formatted data:

#!/usr/bin/python
import demjson

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

json = demjson.encode(data)
print json

The above code execution result is:

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

decode

Python can use demjson.decode() Function decoding JSON data。The function returns Python Field data type。

grammar

demjson.decode(self, txt)

Instance

The above code execution result is How to decode JSON Object:

#!/usr/bin/python
import demjson

json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

text = demjson.decode(json)
print text

The above code execution result is:

{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}





welookups is optimized for learning.© welookups 2018 -
All Right Reserved and you agree to have read and accepted our term and condition