import json
def dict_generator(indict, pre=None):
pre = pre[:] if pre else []
if isinstance(indict, dict):
for key, value in indict.items():
if isinstance(value, dict):
if len(value) == 0:
yield pre+[key, '{}']
else:
for d in dict_generator(value, pre + [key]):
yield d
elif isinstance(value, list):
if len(value) == 0:
yield pre+[key, '[]']
else:
for v in value:
for d in dict_generator(v, pre + [key]):
yield d
elif isinstance(value, tuple):
if len(value) == 0:
yield pre+[key, '()']
else:
for v in value:
for d in dict_generator(v, pre + [key]):
yield d
else:
yield pre + [key, value]
else:
yield indict
def get_itemboy1(filepath):
tmp = []
file = open(filepath, 'r', encoding='utf-8')
testread = file.readlines()
first_line = testread[0] # 取第一行
# print(first_line)
if first_line.startswith(u'\ufeff'):
mydata = first_line.encode('utf8')[3:].decode('utf8')
file.close()
# 将一个python对象编码成json字符串/编码
testdata = json.dumps(mydata, indent=4, ensure_ascii=False)
# 将JSON格式的字符串解码成python对象/解码
testjson = json.loads(testdata)
print(testjson)
print(type(testjson))
bb = json.loads(testjson)
print(bb)
print(type(bb))
sJOSN = testjson
sValue = json.loads(sJOSN)
for i in dict_generator(sValue):
print(''.join(i[0:-1]), ':', i[-1])
return tmp
if __name__ == "__main__":
#get_itemboy('C:\\Users\\Administrator\\Desktop\\testgo\\testgo0.txt')
get_itemboy1('C:\\Users\\31216\\Desktop\\testgo0.txt')
#C:\Users\31216\Desktop\
json解析任意复杂json(显示层次key与value)
最新推荐文章于 2024-08-30 00:40:30 发布