如何在包含字典和列表的python中迭代json对象

如何在包含字典和列表的python中迭代json对象,json,django,python-2.7,dictionary,Json,Django,Python 2.7,Dictionary,我想循环使用web请求得到的这个json对象 当我尝试在Json上循环时,我无法获取键值对中的预期输出。我想要这样的输出 等等。下面的代码是我的方法,但列表中的字典导致了循环中的问题 我得到的错误是“str”对象没有属性“iteritems”。循环这种数据的正确方法是什么 要获取数据,请使用: for i in data_dic: for k,v in i.iteritems(): print k,v 这适用于您的示例(python 3.5.2),但我不

我想循环使用web请求得到的这个json对象

当我尝试在Json上循环时,我无法获取键值对中的预期输出。我想要这样的输出

等等。下面的代码是我的方法,但列表中的字典导致了循环中的问题

我得到的错误是“str”对象没有属性“iteritems”。循环这种数据的正确方法是什么

要获取数据,请使用:

for i in data_dic:
        for k,v in i.iteritems():
            print k,v

这适用于您的示例(python 3.5.2),但我不知道这是否是最好的方法(我的意思是,可能有一些
json
解析函数更易于使用):

结果是:

for v, k in itms.items():
    if not isinstance(k, list):
        for x, y in k.items():
            print(x,':', y)
    else:
        for i in k:
            for s, m in i.items():
                print(s,':', m)
对于Python2.7。仅从
print

CandidateId : 9124657
BatchId : 456279
QPName : Domestic Data Entry Operator(SSC/Q2212)
CenterName : K
ProjectName : PKKVY
Candidate : Noori sahi 
TrainingProviderName : OrionEdutechPrivateLimited
Percentage : 0
PCName : obtain sufficient information from the customer /client to understand the need and perform initial task
MaxScore : 12
YourScore : 0
PCId : SRC/N3022_PC1
Percentage : 0
PCName : compares transcribed data, as displayed on a visual screen, document and corrects any errors with the source
MaxScore : 15
YourScore : 0
PCId : SRC/N3022_PC10
Percentage : 0
PCName : obtain help or advice from specialist if the problem is outside his/her area of competence or experience
MaxScore : 5
YourScore : 0
PCId : SSC/N3022_PC11

我已经试过了,但输出是一样的。我在循环中遇到了问题,因为列表中有一个字典和另一个字典。你能发布你的代码吗?因为您的JSON响应中没有dict_数据。或者你说的是
PCTestScores
?导入请求,json在[15]:r=requests.get在[16]:data=json.load(r.content)在[17]:键入(数据)输出[17]:dict
for i in data_dic:
        for k,v in i.iteritems():
            print k,v
import json
data = json.loads(request.body)
print data['PCTestScores']
for values in data['PCTestScores']:
    print values
    print values['PCId']
    print values['PCName']
    print values['Percentage']
    print values['MaxScore']
    print values['YourScore']
for v, k in itms.items():
    if not isinstance(k, list):
        for x, y in k.items():
            print(x,':', y)
    else:
        for i in k:
            for s, m in i.items():
                print(s,':', m)
CandidateId : 9124657
BatchId : 456279
QPName : Domestic Data Entry Operator(SSC/Q2212)
CenterName : K
ProjectName : PKKVY
Candidate : Noori sahi 
TrainingProviderName : OrionEdutechPrivateLimited
Percentage : 0
PCName : obtain sufficient information from the customer /client to understand the need and perform initial task
MaxScore : 12
YourScore : 0
PCId : SRC/N3022_PC1
Percentage : 0
PCName : compares transcribed data, as displayed on a visual screen, document and corrects any errors with the source
MaxScore : 15
YourScore : 0
PCId : SRC/N3022_PC10
Percentage : 0
PCName : obtain help or advice from specialist if the problem is outside his/her area of competence or experience
MaxScore : 5
YourScore : 0
PCId : SSC/N3022_PC11
for v, k in itms.items():
    if not isinstance(k, list):
        for x, y in k.items():
            print x,':', y
    else:
        for i in k:
            for s, m in i.items():
                print s,':', m