import json
def parseJson(strJson, keyList, valueList):
print(strJson)
# 传入参数校验
if not strJson or len(keyList) == 0:
return []
for i in range(0, len(keyList)):
try:
val = strJson.get(keyList[i])
if type(val) is dict:
print('开始递归')
# parseJson(val, keyList, valueList)
except:
val = ''
valueList.append((keyList[i], val))
def main():
# testData1 = '{"code" : 0, "msg" : "ok" }'
# keyList = ["code", "msg"]
testData1 = '{"code" : 0, "msg" : {"oldmsg" : "这是旧消息", "newmsg" : "这是新的消息提醒"} }'
keyList = ["code", "oldmsg", "newmsg"]
valueList = []
parseJson(json.loads(testData1), keyList, valueList)
print(valueList)
main()