2014-12-10 3 views
-2

에있는 NSDictionary에 중첩 된 JSON을 구문 분석 Json 중첩 :내가 가진 아이폰 OS

[ 
    { 
     "result":"1", 
     "roleId":4 
    }, 
    { 
     "projectInfo":[ 
      { 
       "result":true 
      }, 
      { 
       "Project":[ 
        { 
         "ProjectId":5378, 
         "ProjectName":"ASAG", 
         "CountryId":146, 
         "ProjectGroupId":743, 
         "Description":"Axel Spinger AG" 
        }, 
        { 
         "ProjectId":5402, 
         "ProjectName":"BIZ", 
         "CountryId":146, 
         "ProjectGroupId":759, 
         "Description":"Bizerba Win 7 BAU" 
        }, 
        { 
         "ProjectId":5404, 
         "ProjectName":"BOM", 
         "CountryId":146, 
         "ProjectGroupId":743, 
         "Description":"Bombardier Transportation ThinApp Migration" 
        }, 
        { 
         "ProjectId":5394, 
         "ProjectName":"REDBULL", 
         "CountryId":149, 
         "ProjectGroupId":762, 
         "Description":"Red Bull Mac Packaging" 
        }, 
        { 
         "ProjectId":5397, 
         "ProjectName":"VHV", 
         "CountryId":146, 
         "ProjectGroupId":743, 
         "Description":"VHV Win7 Migration" 
        } 
       ] 
      } 
     ] 
    } 
] 

나는이 대답 같은 일부 특정 키 값을 얻기 위해 작은 조각으로 분리하는 것입니다 필요가있는 무엇 : How to parse JSON into Objective C - SBJSON

을 내 코드 :

SBJsonParser* jParser = [[SBJsonParser alloc] init]; 
NSDictionary* root = [jParser objectWithString:string]; 
NSDictionary* projectInfo = [root objectForKey:@"projectInfo"]; 
NSArray* projectList = [projectInfo objectForKey:@"Project"]; 
for (NSDictionary* project in projectList) 
{ 
    NSString *content = [project objectForKey:@"ProjectId"]; 
    NSLog(@"%@", content); 
} 

그러나 루트 노드에서 projectInfo을 얻으려고하면 오류가 발생합니다. 내 코드에 문제가 있습니까? 내 JSON을 나눌 예제를 제공해주십시오. 어떤 도움이라도 좋을 것입니다.

+0

무슨 오류가 발생 했습니까? – KerrM

+0

어떤 오류가 있었습니까? 쓸 수 있어요? – saidozcan

+0

오류는 루트 요소가 사전이라는 사실에 기인합니다. 실제로는 사전의 배열입니다. – Alladinian

답변

1

당신 JSON은 중첩 된 배열 같이 포함되어 있습니다. 결과를 얻으려면 사전에 각 콘텐츠를 뱉어 내세요.

작업 코드 :

SBJsonParser* jParser = [[SBJsonParser alloc] init]; 
NSArray* root = [jParser objectWithString:string]; 

NSDictionary* projectDictionary = [root objectAtIndex:1]; 
NSArray* projectInfo = [projectDictionary objectForKey:@"projectInfo"]; 
NSDictionary* projectData = [projectInfo objectAtIndex:1]; 
NSDictionary *projectList = [projectData objectForKey:@"Project"]; 
NSLog(@"\n\n Result = %@",projectList 
    ); 
for (NSDictionary* project in projectList) 
{ 
    NSString *content = [project objectForKey:@"ProjectId"]; 
    NSLog(@"\n Project Id =%@", content); 
} 
+0

잘 작동하는지 확인하십시오 .. – svmrajesh

0

JSON 구조에 따르면 최상위 구조는 사전이 아니라 배열입니다.

이 시도 :

SBJsonParser* jParser = [[SBJsonParser alloc] init]; 
NSArray* root = [jParser objectWithString:string]; 
NSDictionary* projectDictionary = [root objectAtIndex:1]; 
NSArray* projectInfo = [projectDictionary objectForKey:@"projectInfo"]; 
+0

projectInfo도 배열입니다. –

+0

네 말이 맞아. 편집 됨. – mprivat