Python displays results in JSON format under UNIX

Source: Internet
Author: User

When using the command-Number output interface test results, it is found that it cannot be displayed in the desired JSON format. Find out more about Python's own powerful tools.

Directly on the code:

Import=="echo '%s ' | Python-m json.tool" % contentprint os.system (command)

The result of the perfect output JSON format will no longer be copied to the online JSON conversion tool.

A brief description of the JSON module for Python:

The JSON contains two very important functions dumps and loads

Json.dumps (str) Converts the Python data type str into a JSON string

Json.loads (str) converts a JSON string into a Python data type

Additional command mate output when using functions as function

For example Json.dumps (Paras,[sort_keys,indent])

Where Sort_keys is the JSON string is sorted by key, indent is the result of each line of indentation processing

>>> Import json>>> data = {' B ': 2, ' a ': 1, ' C ':2}>>> json.dumps (data,sort_keys=true) ' {"A": 1, " B ": 2," C ": 2} '
>>> Import json>>> data = {' B ': 2, ' a ': 1, ' C ':2}>>> json.dumps (data,sort_keys=true,indent=2) ' {\ n  ' A ': 1, \ n  ' B ': 2, \ n  ' C ': 2\n} '

  

  

This article references: http://www.cnblogs.com/kaituorensheng/p/3877382.html;http://www.cnblogs.com/wangyayun/p/6699184.html?utm_ Source=tuicool&utm_medium=referral

Python displays results in JSON format under UNIX

Related Article

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.