python json格式参数遍历所有key、value 及替换key对于的value

时间:2024-03-06 13:13:05

1.对于接口自动化测试,一般接口以json形式发送返回,往往我们就需要遍历json文件中所有key,value以及修改替换key对于的value。

例如json发送/接收的文件:

SendRegisterVerificationCodejson_txt = """
{
  "header":{
    "funcNo": "IF010002",
    "opStation": "11.11.1.1",
    "appId": "aaaaaa",
    "deviceId": "kk",
    "ver":"wx-1.0",
    "channel": "4"
  },
  "payload": {
    "mobileTel": "13817120001"
  }
}
"""

需要查找所有的报文中的key,value,这个json是字典包含字典的形式,如遍历字典后,依旧含有 key对应的value值是字典,需要继续遍历,此时需要用递归进行处理

代码如下:

 1 import json
 2 #json文件发送形式
 3 SendRegisterVerificationCodejson_txt = """
 4 {
 5   "header":{
 6     "funcNo": "IF010002",
 7     "opStation": "11.11.1.1",
 8     "appId": "aaaaaa",
 9     "deviceId": "kk",
10     "ver":"wx-1.0",
11     "channel": "4"
12   },
13   "payload": {
14     "mobileTel": "13817120001"
15   }
16 }
17 """
18 date_json = json.loads(SendRegisterVerificationCodejson_txt)
19 print(date_json)
20 print("*"*10)
21 # 发送时,每次需要注册新的手机号码,就需要json每次提示mobileTel的value进行发送
22 #遍历json文件所有的key对应的value
23 dic ={}
24 def json_txt(dic_json):
25     if isinstance(dic_json,dict): #判断是否是字典类型isinstance 返回True false     
26         for key in dic_json:
27             if isinstance(dic_json[key],dict):#如果dic_json[key]依旧是字典类型
28                 print("****key--:%s value--: %s"%(key,dic_json[key]))
29                 json_txt(dic_json[key])
30                 dic[key] = dic_json[key]
31             else:
32                 print("****key--:%s value--: %s"%(key,dic_json[key]))
33                 dic[key] = dic_json[key]
34                 
35 json_txt(date_json)
36 print("dic ---: "+str(dic))

输出结果:

data_json:  {\'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'payload\': {\'mobileTel\': \'13817120001\'}}
data_json:  {\'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'payload\': {\'mobileTel\': \'13817120001\'}}
**********
****key--:header ,value--: {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}
****key--:funcNo ,value--: IF010002
****key--:opStation ,value--: 11.11.1.1
****key--:appId ,value--: aaaaaa
****key--:deviceId ,value--: kk
****key--:ver ,value--: wx-1.0
****key--:channel ,value--: 4
****key--:payload ,value--: {\'mobileTel\': \'13817120001\'}
****key--:mobileTel ,value--: 13817120001
dic ---: {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\', \'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'mobileTel\': \'13817120001\', \'payload\': {\'mobileTel\': \'13817120001\'}}

 

2.对于接口测试,json主体内容不变,一般我们都是修改json内key对于的value值进行测试,这个接口是手机号注册的接口,正常情况我们都是修改手机号进行测试;

如将key --\'mobileTel\'对于的value-- \'13817120001\'修改,假如修改为13333333333;

那我们需要在遍历json字典key值后,比对如果遍历到对于的key则修改其value。

 def  check_json_value(dic_json,k,v):
    if isinstance(dic_json,dict):
        for key in dic_json:
            if key == k:
                dic_json[key] = v
            elif isinstance(dic_json[key],dict):
                check_json_value(dic_json[key],k,v)
print("date_json 变更前   :")
print(date_json)
check_json_value(date_json,\'mobileTel\',\'13333333333\')
print("date_json 变更后   :")
print(date_json)

完整代码:

 1 import json
 2 """
 3 实现:
 4 1.json_txt(dic_json):json格式,遍历key,value,存储到字典中
 5 2.heck_json_value(dic_json,k,v)json格式,遍历后,替换key的value值
 6 3.data_Json(Sendjson_txt),将json字符串转化为json格式
 7 """
 8 
 9 #json序列化json格式
10 def data_Json(Sendjson_txt):
11     data_json = json.loads(Sendjson_txt)
12     print(\'data_json: \',data_json)
13     return data_json
14 
15 #遍历json文件所有的key对应的value,存储到一个字典中
16 dic ={}
17 def json_txt(dic_json):
18     if isinstance(dic_json,dict): #判断是否是字典类型isinstance 返回True,false     
19         for key in dic_json:
20             if isinstance(dic_json[key],dict):#如果dic_json[key]依旧是字典类型
21                 print("****key--:%s ,value--: %s"%(key,dic_json[key]))
22                 #递归调用
23                 json_txt(dic_json[key])
24                 dic[key] = dic_json[key]
25             else:
26                 print("****key--:%s ,value--: %s"%(key,dic_json[key]))
27                 dic[key] = dic_json[key]
28     
29 
30 #遍历json字典key值后,查到ke则修改值value
31 def  check_json_value(dic_json,k,v):
32     if isinstance(dic_json,dict):
33         for key in dic_json:
34             if key == k:
35                 dic_json[key] = v
36             elif isinstance(dic_json[key],dict):
37                 check_json_value(dic_json[key],k,v)
38     
39 if __name__=="__main__":
40     #json文件发送形式
41     Sendjson_txt = """
42     {
43       "header":{
44         "funcNo": "IF010002",
45         "opStation": "11.11.1.1",
46         "appId": "aaaaaa",
47         "deviceId": "kk",
48         "ver":"wx-1.0",
49         "channel": "4"
50       },
51       "payload": {
52         "mobileTel": "13817120001"
53       }
54     }
55     """
56     #格式化
57     data_json = data_Json(Sendjson_txt)
58     print(\'data_json: \',data_json)
59     print("*"*10)
60     #
61     json_txt(data_json)
62     print("dic ---: "+str(dic))
63 
64     #
65     print("data_json 变更前   :")
66     print(data_json)
67     check_json_value(data_json,\'mobileTel\',\'13333333333\')
68     print("data_json 变更后   :")
69     print(data_json)

执行结果:

data_json:  {\'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'payload\': {\'mobileTel\': \'13817120001\'}}
data_json:  {\'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'payload\': {\'mobileTel\': \'13817120001\'}}
**********
****key--:header ,value--: {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}
****key--:funcNo ,value--: IF010002
****key--:opStation ,value--: 11.11.1.1
****key--:appId ,value--: aaaaaa
****key--:deviceId ,value--: kk
****key--:ver ,value--: wx-1.0
****key--:channel ,value--: 4
****key--:payload ,value--: {\'mobileTel\': \'13817120001\'}
****key--:mobileTel ,value--: 13817120001
dic ---: {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\', \'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'mobileTel\': \'13817120001\', \'payload\': {\'mobileTel\': \'13817120001\'}}
data_json 变更前   :
{\'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'payload\': {\'mobileTel\': \'13817120001\'}}
data_json 变更后   :
{\'header\': {\'funcNo\': \'IF010002\', \'opStation\': \'11.11.1.1\', \'appId\': \'aaaaaa\', \'deviceId\': \'kk\', \'ver\': \'wx-1.0\', \'channel\': \'4\'}, \'payload\': {\'mobileTel\': \'13333333333\'}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------

PS:正在写一个处理接口的逻辑,感谢原文作者的分享:https://www.cnblogs.com/HZQHZA/p/7301362.html