python_jsonpath method

jsonpath provides a convenient way to find a key in a multidimensional dictionary, you must know oh ~

Usage is as follows:

Import jsonpath 
# This is a complex json string D
= { " code " : 200 is , " Message " : " ! success " , " Result " : [{ " SID " : " 29,668,676 " , " text " : " If employees than boss money " , " of the type " : " Video " , "thumbnail": "http://wimg.spriteapp.cn/picture/2019/0719/5d3140efefed1_wpd.jpg", "video": "http://uvideo.spriteapp.cn/video/2019/0719/5d3140efefed1_wpd.mp4", "up": "106", "down": "8", "forward": "2", "comment": "6", "uid": "20272202", "name": "淼兔兔i", "header": "http://wimg.spriteapp.cn/profile/large/2018/04/19/5ad85b1dc8973_mini.jpg", "passtime": "2019-07-21 02:55:02" }, { "sid": "29640878", "text": "", "Vinegar major manufacturers will soon face the biggest ever threattype": "video", "thumbnail": "http://wimg.spriteapp.cn/picture/2019/0708/5d23510ce5a84_wpd.jpg", "video": "http://uvideo.spriteapp.cn/video/2019/0708/5d23510ce5a84_wpd.mp4", "up": "294", "down": "6", "forward": "0", "comment": "21", "uid": "23133772", "name": "雪碧与可乐", "header": "http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1da0563a951_mini.jpg", "passtime": "2019-07-21 02:15:01" }] }
# If we need to find the value of the video, the conventional method is 
Print (D [Result] [0] [video])
 # use jsonpath 
Result = jsonpath.jsonpath (D, ' $ .. video ' )
 # $ outermost representatives, whether this key location can be found where. If a plurality of corresponding Key, then returns a list
= D2 {
     " STU " : {
         " Sex " : ' M ' ,
         " House " : {
             " Bejing " : { " Ring " : 5, " tricyclic " : 4 },
             " Shanghai " : { " Pudong " : . 4 } 
        } 
    } 
} 
Print (D2 [ ' STU ' ] [ 'house'][' Shanghai ' ] [ ' Pudong ' ]) 
result2 = jsonpath.jsonpath (D2, ' $ .. Pudong ' )     # Whether this can be found in key locations where 
result3 = jsonpath.jsonpath (D2, ' $ .stu.house ' ) # $ on behalf of the outermost layer, you can always point down 
# If you can not find the corresponding key, the result is False

 

Guess you like

Origin www.cnblogs.com/hancece/p/11250025.html