写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')
04-30 21:06