Introduction to the use of file JSON JSON: JSON, full name JavaScript Object Notation, is a lightweight data interchange format. The most extensive application of JSON is the data format for the communication of Web servers and clients in Ajax. It is also often used in HTTP requests, so it is natural to learn a variety of JSON. And we're going to take the two most long-run ways we usually work. JSON online parsing and formatting URLs: json.cn
Python |
Json |
Dict |
Object |
List, tuple |
Array |
STR, Unicode |
String |
int, long, float |
Number |
True |
True |
False |
False |
None |
Null |
#json有四种方法: # json.loads# json.dumps# Multi-S is to deal with the string, no more s is to process the file's # json.load# Json.dump #load或者loads word means: load, is to convert JSON into other formats, such as strings or file-related #dump or dumps words that mean upside down, which is to convert other objects or formats into JSON format
#Example 1: Convert Python dict format to JSON string formatImportJsona= Dict (name ='yangling', age = 28,message ='You is so cool')Print(a)Print(Type (a)) b=Json.dumps (a)Print(b)Print(type (b))#Example 2:c =Json.loads (b)Print(Type (c))Print((c))Print(c['name'])
Printing results are:
2. Conversion between file and JSON import JSON #文件相关的话 #load must be out of the file JSON data, load must be to convert the file to JSON data #dump is to write the JSON data into the file # write the JSON into the file dump
1Jsondata ='{\ "a\": 1,\ "b\": 2,\ "c\": 3,\ "d\": 4,\ "e\": 5}'2With open ('a.txt','W') as F:3 json.dump (jsondata,f)4 5With open ('a.txt','R') as fr:6m =Json.load (FR)7 Print(m)8 Print(Type (m))
Printing results are:
Write a a.txt file with the contents: "{\" a\ ": 1,\" b\ ": 2,\" c\ ": 3,\" d\ ": 4,\" e\ ": 5}"
Day18--json