1. Json.loads () converts a JSON string into a "dictionary" type in Python
Import JSON
#json串是一个字符串
f = open (' Product.json ', encoding= ' utf-8 ')
Res=f.read ()
Product_dic=json.loads (RES)
Print (Product_dic)
Operation Result:
2, Json.load () read the file for processing, the same is to convert the JSON string in the file to a dictionary type
Import JSON
#json串是一个字符串
f = open (' Product.json ', encoding= ' utf-8 ')
Product_dic=json.load (f)
Print (Product_dic)
3. Json.dumps () converts the dictionary data into a string in Python
Import JSON
Dict = {' username ': ' Sammy '}
Res=type (Json.dumps (dict))
Print (' Data type:%s '%res)
Operation Result:
Python in Json.dumps (d, Ensure_ascii=false, indent=4, Sort_keys=true)
Ensure_ascii=false indicates that the output Chinese needs to specify the ENSURE_ASCII parameter to False
Indent=4 indicates how much indentation
Sort_keys indicates whether to align
4. Json.dump () turns the dictionary data into a string in Python and writes its results to a file
Import JSON
Dict = {' username ': ' Sammy '}
Json.dump (Dict,open (' A.json ', ' W ', encoding= ' utf-8 '))
Operation Result:
Generate a A.json file
Method functions for working with JSON files in Python