Python实现递归解析json

        在日常自动化测试中,json解析是经常要做的事情。但是有的json格式嵌套复杂,如果按照顺序解析的方法,需要写非常冗长的if判断,有以下几个缺点:1、无复用性:对于其他json格式或者当前格式稍微修改,就需要再写一套;2、健壮性差:冗长的判断极易写错。

        下面是一个原始json串和经过json解析工具(http://app.baidu.com/app/enter?appid=159018)处理的json串:

{"b":{"a":[{"n1":"WIFI","lo":116.30744414106923,"t2":"1387873418.195T+08:00","t3":"target_首页-海报视频点击","p1":"com.tudou.ui.activity.HomeActivity","n2":840,"la":39.98049465154441,"l":false},{"n1":"WIFI","lo":116.30744414106923,"t2":"1387873415.880T+08:00","t1":"A1005","s1":"5da19f89080af666bc2cb8d8775706df","p1":"com.tudou.ui.activity.HomeActivity"}]},"h":{"i":{"o2":"4.3","o1":"Android","b2":"Nexus 7","m":"10:bf:48:c2:81:f5","h":1205,"w":800,"u":"f835c7f8-c331-4b47-a6a3-772021544aa9","b1":"google"}}}

{
	"b":{
		"a":[
			{
				"n1":"WIFI",
				"lo":116.30744414106923,
				"t2":"1387873418.195T+08:00",
				"t3":"target_首页-海报视频点击",
				"p1":"com.tudou.ui.activity.HomeActivity",
				"n2":840,
				"la":39.98049465154441,
				"l":false
			},
			{
				"n1":"WIFI",
				"lo":116.30744414106923,
				"t2":"1387873415.880T+08:00",
				"t1":"A1005",
				"s1":"5da19f89080af666bc2cb8d8775706df",
				"p1":"com.tudou.ui.activity.HomeActivity"
			}
		]
	},
	"h":{
		"i":{
			"o2":"4.3",
			"o1":"Android",
			"b2":"Nexus 7",
			"m":"10:bf:48:c2:81:f5",
			"h":1205,
			"w":800,
			"u":"f835c7f8-c331-4b47-a6a3-772021544aa9",
			"b1":"google"
		}
	}
}

        仔细分析下json格式,无外乎就是字典套列表,列表套字典,层层嵌套,可以用递归实现。

    def print_keyvalue_all(self,input_json):
        key_value=''
        if isinstance(input_json,dict):
            for key in input_json.keys():
                key_value = input_json.get(key)
                if isinstance(key_value,dict):
                    self.print_keyvalue_all(key_value)
                elif isinstance(key_value,list):
                    for json_array in key_value:
                        self.print_keyvalue_all(json_array)
                else:
                    print str(key)+" = "+str(key_value)
        elif isinstance(input_json,list):
            for input_json_array in input_json:
                self.print_keyvalue_all(input_json_array)

        需要注意的是isinstance内置函数的使用,判断当前对象是列表还是字典,非常简洁。

        下面是输出结果:

u = f835c7f8-c331-4b47-a6a3-772021544aa9
w = 800
b2 = Nexus 7
h = 1205
m = 10:bf:48:c2:81:f5
o2 = 4.3
o1 = Android
b1 = google
p1 = com.tudou.ui.activity.HomeActivity
lo = 116.307444141
n1 = WIFI
n2 = 840
la = 39.9804946515
t2 = 1387873418.195T+08:00
t3 = target_首页-海报视频点击
l = False
p1 = com.tudou.ui.activity.HomeActivity
lo = 116.307444141
s1 = 5da19f89080af666bc2cb8d8775706df
t2 = 1387873415.880T+08:00
t1 = A1005
n1 = WIFI

        下面是给定key,输出value的递归实现。

    def print_keyvalue_by_key(self,input_json,key):
        key_value=''
        if isinstance(input_json,dict):
            for json_result in input_json.values():
                if key in input_json.keys():
                    key_value = input_json.get(key)
                else:
                    self.print_keyvalue_by_key(json_result,key)
        elif isinstance(input_json,list):
            for json_array in input_json:
                self.print_keyvalue_by_key(json_array,key)
        if key_value!='':
            print str(key)+" = "+str(key_value)

    原文作者:递归与分治算法
    原文地址: https://blog.csdn.net/daqingwow/article/details/17993881
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞