python给替换json字符串中的值&通过路径获取json字符串的值

一、内容

替换json字符串中的值 

通过路径获取json字符串的值

二、代码

替换json字符串中的值 

def repalceJson(t_json, value, path):
    """
        通过json路径,替换路径的值
        :param t_json: 原字符串
        :param value: 需要替换的值
        :param <list> path: 替换值的路径
    """
    if len(path) > 1:
        try:
            repalceJson(t_json[int(path[0])], value, path[1:])
        except:
            repalceJson(t_json[path[0]], value, path[1:])

    else:
        t_json[path[0]] = value


def dealPath(path):
    """
        讲path替换为list
        :param path: 原字符串
    """
    paths = path.split(".")
    return paths

if __name__ == '__main__':
    d1 = {'result': 0, 'custmid': 471479, 'create_time': 1667186233215, 'version': 1, 'conflict_custms': [],
          'custm': {'custmid': 471479, 'version': 1, 'name': '一个客户.', 'addr': '山东省建市永川重庆路w座 565890', 'contacts': [],
                    'name_swords': [], 'followers': [{'pid': 2302, 'start_time': 1667186233215}], 'follow_level': '待跟进',
                    'create_pid': 2302, 'create_time': 1667186233215, 'modify_pid': 2302, 'modify_time': 1667186233215,
                    'status': 1, 'attrs': [], 'websites': [], 'properties': [], 'pre_followers': []}, 'similars': [],
          'sames': []}
 
    s_path = 'custm.followers.0.pid'


    s_path = dealPath(s_path)
    repalceJson(d1, 111, s_path)
    print(d1)
View Code

 

通过路径获取json字符串的值

 1 class PathValue:
 2     def __init__(self, json_res, value_path):
 3         """
 4         通过json路径找到需要的值,并存入dic
 5         :param json_res: json响应
 6         :param value_path: 取值路径
 7         """
 8         self.json_res = json_res
 9         self.value_path = value_path
10         self.dic = ''
11 
12     def path_value_dic(self):
13         """
14         如果存在多个值,使用,分隔value_path
15         :return:
16         """
17         l_path = self.value_path.split(",")
18         for path in l_path:
19             self.json_path_value(self.json_res, path)
20 
21     def json_path_value(self, d, s):
22         """
23         根据传入的json路径从响应中取值,存入dic,主要运用了递归函数
24         :param d: 响应Json
25         :param s: Json路径
26         :return: 存入值的字典dic
27         """
28         pahts = s.split(".")
29         if len(pahts) == 1:
30             self.dic = d[pahts[0]]
31             return
32         for p in pahts:
33             if p.isnumeric():
34                 a = d[int(p)]
35                 return self.json_path_value(a, '.'.join(pahts[1:]))
36             a = d[p]
37             return self.json_path_value(a, '.'.join(pahts[1:]))
38 if __name__ == '__main__':
39     d1 = {'result': 0, 'custmid': 471479, 'create_time': 1667186233215, 'version': 1, 'conflict_custms': [],
40           'custm': {'custmid': 471479, 'version': 1, 'name': '一个客户.', 'addr': '山东省建市永川重庆路w座 565890', 'contacts': [],
41                     'name_swords': [], 'followers': [{'pid': 2302, 'start_time': 1667186233215}], 'follow_level': '待跟进',
42                     'create_pid': 2302, 'create_time': 1667186233215, 'modify_pid': 2302, 'modify_time': 1667186233215,
43                     'status': 1, 'attrs': [], 'websites': [], 'properties': [], 'pre_followers': []}, 'similars': [],
44           'sames': []}
45     s_path = 'custm.followers.0.pid'
46     p = PathValue(d1, s_path)
47     p.path_value_dic()
48     print(p.dic)
49     print(type(p.dic))
View Code

 

posted @ 2022-11-02 11:05  sugoi  阅读(369)  评论(0)    收藏  举报