python脚本解析json文件

没写完。但是有效果。初次尝试,写的比较不简洁。。。

比较烦的地方在于:

1,中文编码:

pSpecs.decode('raw_unicode_escape')

2,花括号转义:
{{
#!/usr/bin/python
# -*- coding: UTF-8 -*- import os
import json
import sys 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' def writeServices(jsonObj):
print(jsonObj)
pName = jsonObj["name"];
# pType = jsonObj["type"]; def writeProperty( 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))) def writeEvent(jsonObj):
print(jsonObj) #追加文件内容
def writeFile(str):
with open(md_file, 'a+') as fo:
fo.write(str)
fo.close(); def handleJson(alinkDic):
# print(str(alinkDic)) # services = alinkDic["services"]# print(services)
for k in alinkDic.keys():
list = ["services","events","properties"]
if(k in list) :
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__':
if os.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 配置文件不存在'

python脚本解析json文件的更多相关文章

  1. python脚本将json文件生成C语言结构体

    1.引言 以前用过python脚本根据excel生成相关C语言代码,其实本质就是文件的读写,主要是逻辑问题,这次尝试将json文件生成C语言的结构体. 2.代码 这是一个json文件,生成这个结构体的 ...

  2. shell脚本解析json文件

    安装jq扩展 下载:jq 根据自己系统下载对应的文件 cp jq-linux64 /usr/bin cd /usr/bin mv jq-linux64 jq chmod +x jq 使用方法 假设有个 ...

  3. 利用Python 脚本生成 .h5 文件 代码

    利用Python 脚本生成 .h5 文件 import os, json, argparse from threading import Thread from Queue import Queue ...

  4. Android--------使用gson解析json文件

    ##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...

  5. JAVA简便解析json文件

    JAVA简便解析json文件 首先放上我要解析的json文件: { "resultcode":"200", "reason":"S ...

  6. 使用google-gson类库解析json文件

    使用google-gson类库解析json文件 使用JsonParser解析器来解析字符串和输入流,变成json对象 代码如下: public class Readjson { public stat ...

  7. 安卓解析JSON文件

    安卓解析JSON文件 根据JOSN文件的格式,文件只有两种数据,一是对象数据,以 {}为分隔,二是数组,以[]分隔 以下介绍安卓如何解析一个JSON文件,该文件存放在assets目录下,即:asset ...

  8. Java解析JSON文件的方法

    http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...

  9. Logstash:解析 JSON 文件并导入到 Elasticsearch 中

    转载自:https://elasticstack.blog.csdn.net/article/details/114383426 在今天的文章中,我们将详述如何使用 Logstash 来解析 JSON ...

随机推荐

  1. vue2.0leaflet

    github源码在此,记得点星:https://github.com/brandonxiang/vueleaflet 参考文档:https://korigan.github.io/Vue2Leafle ...

  2. Spring.之.报错:Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException

    Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWo ...

  3. CSS笔记之Grid网格系统

    Grid布局已经不是新鲜的技术了,但一直都是使用了Flex布局,如今需要了边学习边做些常用的笔记.首先grid和flex一样都不支持IE10以下的浏览器 基本布局: 一般是所有子元素都横向排列或者都纵 ...

  4. python 将文件大小转换为human readable 的大小表示

    定义了一个函数, def HRS(size):    units=('B','KB','MB','GB','TB','PB')    for i in range(len(units)-1,-1,-1 ...

  5. Unity shader学习之屏幕后期处理效果之高斯模糊

    高斯模糊,见 百度百科. 也使用卷积来实现,每个卷积元素的公式为: 其中б是标准方差,一般取值为1. x和y分别对应当前位置到卷积中心的整数距离. 由于需要对高斯核中的权重进行归一化,即使所有权重相加 ...

  6. java中JDBC连接Oracle数据库

    package com.xxxx.lunwen.test;import java.sql.*;public class DBUtil { static { try { // 加载Oracle驱动程序 ...

  7. 软工网络15团队作业4——Alpha阶段敏捷冲刺4.0

    软工网络15团队作业4--Alpha阶段敏捷冲刺4.0 1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 成员 昨天已完成 今天计划完成 郭炜埕 ...

  8. Yii Restful api自定义字段

  9. Keep On Movin (贪心)

    #include<bits/stdc++.h> using namespace std; int main(){ int T, n, a;scanf("%d",& ...

  10. 源码下载:74个Android开发开源项目汇总

    1. ActionBarSherlock ActionBarSherlock应该算得上是GitHub上最火的Android开源项目了,它是一个独立的库,通过一个API和主题,开发者就可以很方便地使用所 ...