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