废话不多说。。。这个循环递归真是折磨了我两天。。。
上代码解析一个四层json
# -*- coding:utf-8 -*-
”’
Created on 2019
@author: pt0521
”’
import re
import json as js
import sys
def get_json_nums_of_layers(file_path):
fo=open(file_path,’r’,encoding=’utf-8′)
jsonstr = fo.read()#将json作为整个的字符串读取进去
pos1=list()#用于存放
for m in re.finditer(‘{‘, jsonstr):
pos1.append(m.start())
print(pos1)
pos2=jsonstr.find(‘}’)
print(pos2)
for i in range(len(pos1)):
if pos1[i]>pos2:
return i
else:
return len(pos1)
fo.close()
def get_json(file_path,nums_layer_toget):
obj={}
num=1
if(nums_layer_toget>get_json_nums_of_layers(file_path)):
print(“所要解析的层数超过该json拥有的