One, Python support for JSON
Starting with python2.6, the Python standard library has added support for JSON, which requires only import JSON when manipulating JSON. two, Python objects converted to JSON strings
When converting a Python object to a JSON string, you only need the following knowledge:
Conversion rules for 1.python objects to JSON strings:
Python |
JSON |
Dict |
Object |
List, tuple |
Array |
STR, Unicode |
String |
int, long, float |
Number |
True |
True |
False |
False |
None |
Null |
2. The main use of the following functions:
Json.dumps ()
The specific parameters of the function (see the link in the appendix):
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
Test code:
#构造字典
Python2json = {}
#构造list
listdata = [1,2,3]
python2json["listdata"] = Listdata
Python2json ["strdata"] = "Test python obj 2 json"
#转换成json字符串
json_str = Json.dumps (Python2json)
print Json_str
Conversion results:
{
"listdata": [
1,
2,
3
],
"strdata": "Test Python obj 2 json"
}
Third, JSON string converted to Python object
In the same vein, converting a JSON string into a Python object requires only the following knowledge:
Conversion rules for 1.json strings to Python objects:
JSON |
Python |
Object |
Dict |
Array |
List |
String |
Unicode |
Number (int) |
int, long |
Number (real) |
Float |
True True |
|
False |
False |
Null |
None |
2. The main use of the following functions:
Json.loads ()
The specific parameters of the function (see the link in the appendix):
Json.loads (s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]] ]]) example
Test code:
str = ' {listdata ': [1, 2, 3], "strdata": "Test Python obj 2 json"} '
Json2python = json.loads (str)
print type (JSON 2python)
Conversion results:
<type ' Dict ' >
Iv. Appendix
Https://docs.python.org/2/library/json.html
Http://www.cnblogs.com/loleina/p/5623968.html