Python serialized JSON, Pickle

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.