Converting JSON strings in Python

Source: Internet
Author: User
When we develop a Web service, we may use a JSON-based Web service protocol. If you use the Python language for development, its extensions are messages that can be processed directly in JSON format. For example, the Python JSON module introduced in Python2.6 provides the default JSON encoder and decoder, and of course you can install and use other JSON encoders/decoders.

The following code snippet is an example of parsing json in Python

Import JSON json_input = ' {"One": 1, "one": {"list": [{"Item": "A"},{"item": "B"}]} ' try:    decoded = json.loads (js On_input)     # Pretty printing of json-formatted string    print Json.dumps (decoded, Sort_keys=true, indent=4)     Print "JSON parsing example:", decoded[' one ' "    print" Complex JSON parsing Example: ", decoded['-'" [' List '][1][' ite M '] except (ValueError, Keyerror, TypeError):    print "JSON format error"

Here is the result of the example printing

{    "one": 1, "one":    {"        list": [            {                "item": "A"            },            {                "item": "B"            }        ]    }} JSON parsing example:  1Complex JSON parsing example:  B
  • 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.