#Auther Bob
#--*--conding:utf-8--*--
#jshon这个模块就是做序列化处理的, four ways to use the JSON module mainly
#1, dumps
#2, loads
#3, dump
#4, load
#先介绍dumps方法
#通过jshon的dumps的模块可以把特定的对象序列化处理为字符串
# import JSON
# L1 = [1,2,3,454]
# D1 = {' K1 ': ' v1 '}
# ret = json.dumps (L1)
# Print (Type (ret))
# ret = json.dumps (D1)
# Print (Type (ret))
# <class ' str ' >
# <class ' str ' >
# L1 = ' [1,2,3,4] '
# D1 = ' {"K1": "V1"} '
# print (Type (L1))
# print (Type (D1))
#在来介绍loads方法
#上面的l1和d1都是字符串, but they look like List and dict, and we can convert these 2 strings to list and dict by deserializing, if
#外形不是list或者dict的形状, the success is not converted
# ret = json.loads (L1)
# Print (Ret,type (ret))
# ret = json.loads (D1)
# Print (Ret,type (ret))
# [1, 2, 3, 4] <class ' list ' >
# {' K1 ': ' v1 '} <class ' Dict ' >
#来做一个小练习, through the third-party module get to the HTTP request, and then the JSON module returns the string structure of the data into the form of a dictionary, so that we can
#对这个字典做操作
# import Requests
# import JSON
#
# ret = requests.get (' http://wthrcdn.etouch.cn/weather_mini?city= Beijing ')
# ret.encoding = ' Utf-8 '
# S1 = Ret.text
# Print (S1,type (S1))
#拿到字符串形式的数据
# {"desc": "Invilad-citykey", "Status": 1002} <class ' str ' >
#
# D1 = json.loads (S1)
# Print (D1,type (D1))
#通过loads的方法, convert the string into a dictionary
# {' desc ': ' Invilad-citykey ', ' Status ': 1002} <class ' Dict ' >
#上面的dumps和loads方法都在内存中转换, the following dump and load methods will be one more step, dump is to write the serialized string into a file, and
#load是从一个一个文件中读取文件
#然后来介绍dump方法
# import JSON
# D1 = {' name ': ' Foot '}
#这一步就会把d1做序列化处理后的字符串写到db这个文件中
# Json.dump (D1,open (' db ', ' W '))
# D1 = json.load (open (' db ', ' r '))
# Print (D1,type (D1))
# {' name ': ' Foot '} <class ' Dict ' >
Introduction to the Dumps,loads,dump,load method for Python JSON modules