python之json格式的读写


以下jsondata.json文件:先生成一个txt文件,再修改文件后缀,
如:txt文件内容为:
{"GetCityInfo":{'CommandCode': 'GetAllCityData', 'MarKer': '1482738389646', 'TransferData': '{"CityId":430100}'}}

import json
class Operate_Json():
def __init__(self,fileName=None):
if fileName !=None:
self.fileName = fileName
else:
self.fileName = "../dataconfig/jsondata.json"

##读取json文件
def read_json(self):
with open(self.fileName) as fp:
return json.load(fp)
##获取指定key的值
def get_value_by_key(self,key):
return self.read_json()[key]

if __name__=="__main__":
oj = Operate_Json()
print(oj.get_value_by_key("GetCityInfo"))
posted @ 2021-01-11 00:05  竹贤  阅读(484)  评论(0编辑  收藏  举报