python解析json文件三种_python脚本解析json文件

#!/usr/bin/python#-*- coding: UTF-8 -*-

importosimportjsonimportsys

reload(sys)

sys.setdefaultencoding("utf-8")

json_file= 'alink.json' #读文件

md_file = 'alink.md' #写文件

#写入模版

protocol_templete ='## {pName}\n### [Format]\n```json\n {{\n "{property}":\"\"\n }}\n```\n### [Parameters]\n* {property};{pType};属性说明.\n* specs:{pSpecs}\n\n\n'

defwriteServices(jsonObj):print(jsonObj)

pName= jsonObj["name"];#pType = jsonObj["type"];

defwriteProperty( jsonObj ):

pName= jsonObj["name"];

property= jsonObj["identifier"];

pType= jsonObj["dataType"]["type"]

pSpecs= json.dumps(jsonObj["dataType"]["specs"])print(pSpecs.decode('raw_unicode_escape')) //解决中文编码问题#print(protocol_templete.format(pName="".join(pName),pType=pType,pSpecs=pSpecs,property="".join(property)))

#writeFile(protocol_templete)

writeFile(protocol_templete.format(pName="".join(pName),pType=pType,pSpecs=pSpecs.decode('raw_unicode_escape'),property="".join(property)))defwriteEvent(jsonObj):print(jsonObj)#追加文件内容

defwriteFile(str):

with open(md_file,'a+') as fo:

fo.write(str)

fo.close();defhandleJson(alinkDic):#print(str(alinkDic)) # services = alinkDic["services"]# print(services)

for k inalinkDic.keys():

list= ["services","events","properties"]if(k inlist) :

writeFile("## %s\n"%k)

values= alinkDic[k] #list

if(k == "services"):

map(writeServices,values)elif(k == "events"):

map(writeEvent,values)else:

map(writeProperty,values)if __name__ == '__main__':ifos.path.exists(json_file):

fileContent=open(json_file).read();#清空文件

with open(md_file, 'wb+') as file:

file.close();

jsonDic= json.loads(fileContent) #print(open(json_file).read()); //打印json文件

handleJson(jsonDic) #print(json.loads(''.join(open(json_file).readlines()))) //json对象转换成python对象

else:print 'json 配置文件不存在'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值