2014-04-13 8 views
2

나는 시스코 API로 작업해야하므로 json을 시도하고 json 객체를 통해 루프하는 방법을 알아낼 수 없다. 내 키를 얻을 수는 있지만 값을 얻을 수 없다.Python and Json

내가 나에게 http://www.jsoneditoronline.org/는 JSON 형식을 이해하고 사용하고

그러나 이것은 내가 지금까지 무엇을 가지고 ..

는 JSON 파일 :

{ 
    "queryResponse" : { 
    "@rootUrl" : "\/webacs\/data", 
    "@requestUrl" : "https : \/\/192.168.116.207\/webacs\/api\/v1\/data\/DeviceGroups\/42", 
    "@responseType" : "getEntity", 
    "entity" : { 
     "@url" : "\/webacs\/data\/className\/15", 
     "@type" : "className", 
     "@dtoType" : "deviceGroupsDTO_$$_javassist_5196", 
     "deviceGroupsDTO" : { 
     "@id" : "15", 
     "@displayName" : "String value", 
     "clearedAlarms" : 1, 
     "criticalAlarms" : 1, 
     "groupId" : 2, 
     "groupName" : "String value", 
     "informationAlarms" : 1, 
     "majorAlarms" : 1, 
     "minorAlarms" : 1, 
     "name" : "String value", 
     "warningAlarms" : 1 
     } 
    } 
    } 
} 

내 파이썬 스크립트 :

import json 

jsondata = json.load(open('data.json')) 

for rows in jsondata['queryResponse']['entity']['deviceGroupsDTO']: 
    print(rows) 

그것을 인쇄의 :

name 
@id 
warningAlarms 
@displayName 
informationAlarms 
clearedAlarms 
majorAlarms 
groupId 
groupName 
criticalAlarms 
minorAlarms 

내가 무엇을 잘못하고 있는지 확실하지 않습니다 ...

답변

2

jsondata['queryResponse']['entity']['deviceGroupsDTO']은 사전입니다. items() 이상

으로 반복 키, 값 쌍을 얻을 수 있습니다 : python2의 경우, 당신은 더 나은 items() 대신에 iteritems()를 사용하는 것이라고

for key, value in jsondata['queryResponse']['entity']['deviceGroupsDTO'].items(): 
    print(key, value) 

참고.

은 참조 : 정화에 대한 What is the difference between dict.items() and dict.iteritems()?

+0

감사합니다! – SKTLZ