The dumps and loads of Python json

Source: Internet
Author: User
Encoding: Converts a Python object encoding into a JSON string

Decoding: Convert JSON format string decoding to Python object


Json.dumps for simple data types encoding

Json.loads processing decoding conversions for simple data types

Look at the example below to show clearly the difference between dumps and loads.

Import JSON
Data=[{' A ': "A", ' B ':(2,4), ' C ': 3.0}]//list data type
Print data
Output is: [{' A ': ' A ', ' C ': 3.0, ' B ': (2, 4)}]
Print len (data)
Output 1
Print Data[0]
Output {' A ': ' A ', ' C ': 3.0, ' B ': (2, 4)}


Coding
Data_dump=json.dumps (data)//encoding
Print Len (data_dump)//converted to string
Output 35
Print Data_dump
Output [{"A": "A", "C": 3.0, "B": [2, 4]}]
Print Data_dump[0]
Output: [

to decode
Data_loads=json.loads (Data_dump)
Print Data_loads
Output: [{u ' a ': U ' a ', U ' C ': 3.0, U ' B ': [2, 4]}]
Print Len (data_loads)
Output 1
Print Data_loads[0]
Output: {u ' a ': U ' a ', U ' C ': 3.0, U ' B ': [2, 4]}



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.