python接口测试学习(4)之json/dict类型转换

json: JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式

JSON 语法规则

JSON 语法是 JavaScript 对象表示语法的子集。

  • 数据在名称/值对中
  • 数据由逗号分隔
  • 大括号保存对象
  • 中括号保存数组

MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型

dict数据类型

#  coding:utf-8
import json
data = {
    'id' : 1,
    'name' : 'test1',
    'age' : '1',
    'full' : None
}
jsons = '{"name": "Ann", "age": "34", "result": 123}'
json_str = json.dumps(data, indent=4)         # dictjson indent=4输出格式优化
dict_str = json.loads(jsons)        # jsondict
print(json_str)
print(dict_str)

def dict_to_json_flie():
    dict = {}
    dict['name'] = 'Haaaaa'
    dict['age'] = '18'
    dict['sex'] = 'M'
    with open('1.json', 'w') as f :
        json.dump(dict, f, indent=4)
dict_to_json_flie()

def json_to_dict_file():
    with open('1.json', 'r') as f:
        print(json.load(fp=f))

输出


猜你喜欢

转载自blog.csdn.net/u013783095/article/details/80434637