写 case 时, 将 case 写到 JSON 文件比写到, 写 python 一定要学会处理 JSON
以下, 是要处理的 JSON
处理操作包括: 打开 JSON 文件, 获取 JSON 文件内容, 关闭 JSON 文件, 读取内容中的对应 key 的 value
- {
- "name": "BeJson",
- "url": "http://www.bejson.com",
- "page": 88,
- "isNonProfit": true,
- "address": {
- "street": "科技园路.",
- "city": "江苏苏州",
- "country": "中国"
- },
- "links": [
- {
- "name": "Google",
- "url": "http://www.google.com"
- },
- {
- "name": "Baidu",
- "url": "http://www.baidu.com"
- },
- {
- "name": "SoSo",
- "url": "http://www.SoSo.com"
- }
- ]
- }
python 实现:
- #coding=utf-8
- import JSON
- class OperationJson:
- def __init__(self,file_name=None):
- if file_name:
- self.file_name = file_name
- else:
- self.file_name = './dataConfig/data.json'
- self.data = self.get_data()
- def get_data(self):
- fp = open(self.file_name)
- data = JSON.load(fp)
- fp.close()
- return data
- def get_value(self,id):
- return self.data[id]
- if __name__ == '__main__':
- opers = OperationJson()
- print opers.get_value('name')
来源: http://www.bubuko.com/infodetail-3343412.html