json 模块

pickle 和  shevle  序列化后得到的数据只有python才能够解析

通常企业开发不可能做一个单机程序 都需要联网进行计算机间的交互

我们必须保证这个数据能够跨平台使用

 

  JSON是什么?   java scrip object  notation

  var obj  =  {"name" :"egon"}

  对于我们开发而言  json 就是一种通用的数据格式   任何语言都能解析

 js 中的数据类型         python数据类型  的对应关系

    {}                             字典

    []        列表

  string  “ ”                          str

  int/float                            int/float

  true/false       True/False

     null                                 None

  

  json 格式的语法规范

  最外层通常是一个字典或列表

  {}    or   []

  只要你想写一个json格式的数据  那么最外层直接写{}

  字符串必须是双引号

  你可以在里面套人一多的层次

 

  json模块的核心功能

  dump

  dumps

  load

  loads

  不带s  封装write    和read

 

 

import json

# 反序列化
# with open("a.json","rt",encoding="utf-8") as f:
# res = json.loads(f.read())
# print(type(res))

# with open("a.json",encoding="utf-8") as f:
# print(json.load(f))


# 直接解析字符串的json为python对象

jsontext = """{
"users": [{
"name": "agon",
"age": 68
},
{
"name": "agon",
"age": 68
}
]
}"""

# res = json.loads(jsontext)
# print(res)


mydic = {
"users": [{
"name": "agon",
"age": 68
},
{
"name": "agon",
"age": 68
}
]
}
# with open("b.json","wt",encoding="utf-8") as f:
# f.write(json.dumps(mydic))

# with open("b.json", "wt", encoding="utf-8") as f:
# json.dump(mydic, f)

import json

# dic = {"a": '理查德姑妈', "b": "找到你", "c": "看不见的客人"}
# with open("c.json","wt",encoding="utf-8") as f:
# f.write(json.dumps(dic))
# print(repr(s), type(s))

# with open("c.json","rt",encoding="utf-8") as f:
# # print(f.read())
# d = json.loads(f.read())
# print(d)

 

转载于:https://www.cnblogs.com/frank007/p/9806845.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值