python 类json数据的解析和美化输出

起因是python的json库没法使用,原因大概是接收到的字符串不是标准的json数据格式,无法转化为python的字典类型,一调用json.loads()就报错,被迫营业打算自己写个规则

说一下自己的思路

其实要解决的无非是两个问题,一个是换行的时机,一个是缩进的数量

一开始想到递归,可是掂量了一下自己,果断放弃,而且也担心递归的效率(后来其实也试过,但是发现根本不需要)

然后想到以括号和逗号为分隔符将整个数据打散成列表,一个一个按顺序输出,然后同样以括号为标志决定换行和换行后的缩进量,其实这个思路挺合理的,而且可以预想规则会非常简单;只是一旦里面的括号多了,会看到大量的独占一行的括号,数据会显得非常松散,如果想改进,就得判断括号前后的数据是键值对呢还是括号还是逗号,这种瞻前顾后的设计一点都不好

之后转变了一下思路,没必要老盯着括号不放,键值对先换行再输出不也一样吗,正括号根本不需要考虑换行,逗号也一样,只有反括号为了美观有必要另起一行

至于缩进数量,正括号加,反括号减,由于括号本身是严格对称的,只要按顺序输出,没有什么好担心的

后面经过一番摸索测试,就得到一个不错的效果了,代码如下

import re

def jsonParse(datas, result='', n=0):
    for data in re.split('(\{|\[|\]|\}|,)', datas):
        data = re.sub(r'^\s+|\s+$', '', data)	# 为了更大程度地兼容,保证列对齐
        if data == '':
            continue
        elif data == ',':
            result = result + data
            continue
        elif re.match('^\"', data):
            innerList = re.split('(,)', data)
            inner_row = 0
            for data2 in innerList:
                tabs = n * '\t'
                result = result + f'\n{tabs}{data2}'
                inner_row += 1
            continue
        elif re.match('[\{\[]', data):
            result = result + data
            n += 1
            continue
        elif re.match('[\}\]]', data):
            n -= 1
            tabs = n * '\t'
            result = result + f'\n{tabs}{data}'
            continue
        else:
            result = result + f'{data}'
            continue
    return result

调用示例
# 上点强度
datas = '''			
{"name": "meditation", "symptoms": "strike the wall with head", "code name": SB, 
"CAUTION": ["who am I", "where am I", "wash wo me do"], 
   "steps": [{"Hello son", "Nothing to say", "gg?": no, "MM?": "imposible", "dinosaure": "bingo"},{"haha", "hehe", "xixi"}], 
"Bilibili universal Corperation": {[ 	"How you feel": "boring", 
"what's boring": "s h i f t", 
        	 "repeat": "Hello bigboy",   "shadow": no shadow go die],[
        "axe": "Hatchet ", "river": "rival", 
"threaten": "intimidate", "aggressive": "irritable"]}
  }
'''

result= jsonParse(datas)
print(result)

输出结果

{
	"name": "meditation",
	"symptoms": "strike the wall with head",
	"code name": SB,
	"CAUTION":[
		"who am I",
		"where am I",
		"wash wo me do"
	],
	"steps":[{
			"Hello son",
			"Nothing to say",
			"gg?": no,
			"MM?": "imposible",
			"dinosaure": "bingo"
		},{
			"haha",
			"hehe",
			"xixi"
		}
	],
	"Bilibili universal Corperation":{[
			"How you feel": "boring",
			"what's boring": "s h i f t",
			"repeat": "Hello bigboy",
			"shadow": no shadow go die
		],[
			"axe": "Hatchet ",
			"river": "rival",
			"threaten": "intimidate",
			"aggressive": "irritable"
		]
	}
}

可以看到对齐没有问题,也可以兼容键值对内部的空格

最主要的就算不是标准的json格式也没关系,只要数据以双引号开始,以逗号结尾即可

(缩进和反括号还有改进的空间,觉得没必要就不折腾了)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值