{
"testDict" :{
"testDict_1_string" : "1_value" ,
"testDict_2_int" : one ,
"testDict_3_bollen" : false ,
"testDict_4_list" :[ "4_value" , one ],
"testDict_5_null" : null ,
"testDict_6_dict" :{ "6_key" :{ "6_key_1" : "6_value_1" }, "6_list" :[ "test_list" ]},
"testDict_7_complex" :[
{
"id" : "10000" ,
"testA" :{ "A" : "A_value" },
"testB" :[ "B1" , "B2" ]
},
{
"id" : "10001" ,
"testC" :{ "C" : "C_value" },
"testRepeat" : "R_value"
},
[ "testX" , "testY" , { "testRepeat" : "testRepeat" }],
[ "test_list_2" ],
"7_value"
],
"testRepeat" : "R_value"
},
"testRepeat" : "R_value" ,
"test_extra" :[ "test_extra_value" ]
}
def getKeyFromJsonFile (dic_json, keylist, keyParent=None, isChild=False, listInlist=False) :
#If the acquired data type is dict, traverse the key of the dictionary to obtain the value data type
if isinstance(dic_json, dict):
for key in dic_json:
#Do different processing and recursion according to the value data type
if isinstance(dic_json[key], dict):
#Splice the node path and insert the list
if isChild:
key = keyParent + '-*-' + key
keylist.append(key)
getKeyFromJsonFile(dic_json[key.split( '-*-' )[ -1 ]], keylist, keyParent=key, isChild= True , listInlist= False )
elif isinstance(dic_json[key], list):
if isChild:
key = keyParent + '-*-' + key
keylist.append(key)
for i in dic_json[key.split( '-*-' )[ -1 ]]:
getKeyFromJsonFile(i, keylist, keyParent=key, isChild= True , listInlist= False )
else :
#The key structure in the list may be the same as that in the list
#It only deals with one layer, but lacks practical significance
if listInlist:
key = key + '_*inlist'
if isChild:
key = keyParent + '-*-' + key
keylist.append(key)
#Recursion is required again for [...] contained in [...]
if isChild and isinstance(dic_json, list):
for i in dic_json:
getKeyFromJsonFile(i, keylist, keyParent=keyParent, isChild= True , listInlist= True )
def getValue (slist, data_next) :
#Traverse the key parameter after the partition
for j in range( zero ,len(slist)):
#The data type corresponding to the current key is list
if isinstance(data_next[slist[j]], list):
return
#Not a list
if j == len(slist) -1 :
#Get the value to be modified
value = data_next[slist[j]]
return
#Retrieve in the next level dictionary in each node path cycle
data_next = data_next[slist[j]]
def getValueFromList (data_list, key_list) :
#The element data type in the list is dictionary
if isinstance(data_list, dict):
for key in key_list:
#Retrieve from the passed in key in the dictionary
if key in data_list.keys() and key == key_list[ -1 ]:
#Get the value to be modified
value = data_list[key]
#If there is still a list in dict
if isinstance(data_list[key], list):
#Get the target list that needs to traverse its elements to modify
value = data_list[key]
return
#Continue recursion if there are child nodes
elif key in data_list.keys():
getValueFromList(data_list[key], key_list)
if isinstance ( data_next[slist[j]], list ):
#The current key has no child nodes
if j == len(slist) -1 :
value = data_next[slist[j]]
else :
for datas in data_next[slist[j]]:
tlist = slist[j+ one :]
getValueFromList(datas, tlist)
return
#Common data types of Json fault tolerance
null = None
false = False
type_list = [ "testString" , one , false , null, [ "test_list" ], { "test_dict" : "test_dict_value" }]
#Path is the preset folder path+file name
with open ( path , "w" ) as f:
json.dump( data , f, sort_keys= True , indent= four , ensure_ascii= False )
//Corresponding position in Json
"testDict_7_complex" : [
{
"id" : "10000" ,
"testA" : {
"A" : one
},