Common methods of Python_json

Source: Internet
Author: User

1. What is json?

JSON can convert a set of data represented in a JavaScript object into a string , which can then be easily passed between functions, or in an asynchronous application passing a string from a WEB client to a server-side Program.

2. JSON syntax

      • Data in Key-value pairs
      • Data is separated by commas
      • Curly braces Save Object
      • Square brackets Save Array

3. Common methods of JSON

    • JSON to dictionary conversion: ret_dict = json.loads (json_str)
    • Dictionary to json conversion: json_str = json.dumps (dict)

4. Example

#-*-coding:utf-8-*-Importjsonjson_content='{"name": "test", "type": {"name": "seq", "parameter": ["1", "2"]}}'PrintU"JSON to Dictionary conversion (method one):"L=eval (json_content)PrintLPrintL.keys ()Printl["name"]Printl["type"]["name"]Printl["type"]["parameter"][1]PrintU"JSON to Dictionary conversion (method two):"s=json.loads (json_content)PrintsPrintS.keys ()Prints["name"]Prints["type"]["name"]Prints["type"]["parameter"][1]dict_content= {"name":"Test","type":{"name":"seq","parameter":["1","2"]}}PrintU"Dictionary to JSON"s=json.dumps (dict_content)PrintsTry:    PrintS.keys ()exceptattributeerror:PrintU"object is not a dictionary! "

Note: 1. JSON corresponds to a json-formatted string

Common methods of Python_json

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.