记录把json的所有key转换成小写
Posted IT界的测试混子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录把json的所有key转换成小写相关的知识,希望对你有一定的参考价值。
import json
d = '''
"Type": "Abc",
"Id": "NO_ID",
"IntId": -1,
"Hashcode": 54321,
"Rect":
"Width": 1234,
"Height": 222,
"Left": 0,
"Top": 0
,
"Visible": true,
"Enabled": true,
"Clickable": false,
"Tag": "",
"Desc": "",
"Children": [
"Type": "android.widget.LinearLayout",
"Id": "NO_ID",
"IntId": -1,
"Hashcode": 12345,
"Rect":
"Width": 1234,
"Height": 2220,
"Left": 0,
"Top": 0
,
"Visible": true,
"Enabled": true,
"Clickable": false,
"Tag": "",
"Desc": "",
"Children": [
"Type": "android.view.ViewStub",
"Id": "01234",
"IntId": 2314,
"Hashcode": 22222,
"Rect":
"Width": 0,
"Height": 0,
"Left": 0,
"Top": 0
,
"Visible": false,
"Enabled": true,
"Clickable": false,
"Tag": "",
"Desc": "",
"Children": []
]
]
'''
def change_key_lower(d):
if isinstance(d,dict):
for i in list(d.keys()):
if isinstance(d[i], dict):
change_key_lower(d[i])
if isinstance(d[i],list):
for j in d[i]:
if isinstance(j, dict):
change_key_lower(j)
d[i.lower()] = d.pop(i)
if __name__ == '__main__':
d = json.loads(d)
res = change_key_lower(d)
以上是关于记录把json的所有key转换成小写的主要内容,如果未能解决你的问题,请参考以下文章