What is JSON:
JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy to read and write, but also easy to machine parse and generate. JSON takes a completely language-independent text format, but also uses a C-like family habit such as C,c++,c#,java,javascript,perl,python, which makes JSON an ideal exchange language.
For encoding and decoding of simple data types:
## #encoding>>>ImportJSON>>> obj=[[1,2,3], ('a','b','C'),{'4'75A'6': 7},'def']>>> encoding_json=json.dumps (obj)>>>Print(Encoding_json) [[1, 2, 3], ["a","b","C"], {"4": 5,"6": 7},"def"]#Note that this tuple is converted to a list>>>Print(repr (obj)) [[1, 2, 3], ('a','b','C'), {'4': 5,'6': 7},'def']#this is just converted to a string
>>> Json.dumps ({' B ': 4, ' A ': 5, ' E ': 6})
' {' A ': 5, ' B ': 4, ' E ': 6} '
>>> Json.dumps ({' B ': 4, ' A ': 5, ' e ': 6},sort_keys=true)
' {' A ': 5, ' B ': 4, ' E ': 6} '
>>> A = Json.dumps ({' B ': 4, ' A ': 5, ' e ': 6},sort_keys=true,indent=5) #indent指明缩进为5
>>> Print (a)
{
"A": 5,
"B": 4,
"E": 6
}
There is a conversion from the Python data type to the JSON type during the encoding of the JSON.
Python |
Json |
Dict |
Object |
List,tuple |
Array |
Str |
String |
Int,float |
Number |
True |
True |
False |
False |
None |
Null |
## decode>>>Encoding_json'[ [1, 2, 3], ["A", "B", "C"], {"4": 5, "6": 7}, "Def"]'>>> decoding_json=json.loads (Encoding_json)>>>Print(Decoding_json) [[1, 2, 3], ['a','b','C'], {'4': 5,'6': 7},'def']>>>type (Decoding_json)<class 'List'>>>>Print(decoding_json[2]['4'])5
The data type conversions from JSON to Python are as follows:
Json |
Python |
Object |
Dict |
Array |
List |
String |
Unicode |
Number |
Int,long |
Ture |
True |
False |
False |
Null |
None |
Python serialized JSON, Pickle