Python--day72--json内容回顾

前后端分离,Json格式字符串:序列化和反序列化

 1 """
 2     复习python中的json模块
 3 """
 4 
 5 import json
 6 
 7 s = '{"name":"xiaohei","age":18}'
 8 #把字符串反序列成python中的数据类型
 9 ret = json.loads(s)
10 print(ret,type(ret))
11 
12 #把字典序列化Python中的字符串
13 ret2 = json.dumps(ret)
14 print(ret,type(ret2))

 

合格的json对象:

["one", "two", "three"]
{ "one": 1, "two": 2, "three": 3 }
{"names": ["张三", "李四"] }
[ { "name": "张三"}, {"name": "李四"} ] 

 不合格的json对象:

复制代码
{ name: "张三", 'age': 32 }  // 属性名必须使用双引号
[32, 64, 128, 0xFFF] // 不能使用十六进制值
{ "name": "张三", "age": undefined }  // 不能使用undefined
{ "name": "张三",
  "birthday": new Date('Fri, 26 Aug 2011 07:13:10 GMT'),
  "getName":  function() {return this.name;}  // 不能使用函数和日期对象

 

posted @ 2019-04-10 16:02  莱茵河的雨季  阅读(141)  评论(0编辑  收藏  举报