给定json串,获取项目key的所有值,eg:data='{"code": "ss", "rows":[{"id": 1, "value": "test"},{"id": 2, "value": "ress"}]}',要获取所以id的值,1、2.
代码如下:
#-*-coding: utf8 -*-
import json
data='{"code": "ss", "rows":[{"id": 1, "value": "test"},{"id": 2, "value": "ress"}]}'
data = json.loads(data)
def testJson(data, name):
if type(data).__name__ == 'dict':
if name in data.keys():
print "1. %s" % data[name]
else:
for key in data.keys():
#print 'ssss %s' % data
newData = data[key]
if type(newData).__name__ == 'dict':
data = newData[key]
print 'data1 %s' % data
testJson(data, name)
if type(newData).__name__ == 'list':
for i in range(len(newData)):
tempData = newData[i]
testJson(tempData, name)
if type(data).__name__ == 'list':
for i in range(len(data)):
tempData = data[i]
print 'tempData %s' % tempData
testJson(tempData, name)
testJson(data, 'id')