2012-06-04 2 views
0

배열에 대한 응답보다 아래에 오는 아이폰 코드는 어떻게됩니까? 여기에 "Id 값", "LastUpdated 값"및 "Title"내용을 저장할 필요가 있습니다.이 세 값은 어떻게 3 가지 다른 배열에 저장해야합니까?NSArray에 응답을받는 JSON 구문 분석을위한 코드를 구현하는 방법은 무엇입니까?

[ 
    { 
     "Contents": [ 
      { 
       "Id": 381, 
       "LastUpdated": "/Date(1338347251003+0000)/", 
       "Title": "Forrester Study - Total Economic Impact of Lync", 
       "Url": "http://infra2apps.blob.core.windows.net/eventapp/Forrester Study - Total Economic Impact of Lync_booth1.pdf" 
      } 
     ], 
     "Id": 1, 
     "ImageUrl": "http://infra2apps.blob.core.windows.net/eventapp/Lync.png", 
     "Name": "Unified Communications & Collaborations", 
     "Sessions": [ 
      { 
       "Description": "Microsoft Lync delivers Unified Communications to help people connect in new ways, anytime, anywhere. Learn how HP and Microsoft are helping customers transform their business infrastructure and gain greater productivity by making every communication an interaction that is more collaborative and engaging.", 
       "EndDate": "/Date(1338922800000+0000)/", 
       "FriendlyName": "TB3257", 
       "Id": 1, 
       "Location": "TBD", 
       "Speakers": [ 
        { 
         "Company": "Microsoft", 
         "Email": "[email protected]", 
         "Name": "Ashima Singhal", 
         "Title": "Group Marketing Manager, Lync" 
        }, 
        { 
         "Company": "HP", 
         "Email": "d[email protected]", 
         "Name": "Dragana Beara", 
         "Title": "HP" 
        } 
       ], 
       "StartDate": "/Date(1338920100000+0000)/", 
       "Title": "Connecting People in New Ways with Microsoft Lync" 
      } 
     ] 
    }, 
    { 
     "Contents": [ 
      { 
       "Id": 385, 
       "LastUpdated": "/Date(1338347251143+0000)/", 
       "Title": "Windows 8 Consumer Preview - Product Guide - Business", 
       "Url": "http://infra2apps.blob.core.windows.net/eventapp/Windows 8 Consumer Preview - Product Guide - Business_booth2.pdf" 
      }, 
      { 
       "Id": 383, 
       "LastUpdated": "/Date(1338347251080+0000)/", 
       "Title": "Mitigating Risk - Why Sticking with Windows XP is a Bad Idea", 
       "Url": "http://infra2apps.blob.core.windows.net/eventapp/Mitigating Risk - Why Sticking with Windows XP is a Bad Idea - IDC - May 2012_booth2.pdf" 
      } 
     ], 
     "Id": 2, 
     "ImageUrl": "http://infra2apps.blob.core.windows.net/eventapp/Windows-brand.png", 
     "Name": "Windows the right choice for business", 
     "Sessions": [ 
      { 
       "Description": "Microsoft and HP are partnering together to migrate customers to Windows 7 while working closely together to build new Windows 8 products that will easily integrate into a Windows 7 environment. This session provides insight on how these new offerings will provide enterprise grade solutions with no compromise business devices that increase productivity, security, and manageability and the path to get there.", 
       "EndDate": "/Date(1338935400000+0000)/", 
       "FriendlyName": "TB3256", 
       "Id": 3, 
       "Location": "TBD", 
       "Speakers": [ 
        { 
         "Company": "Microsoft", 
         "Email": "[email protected]", 
         "Name": "Sara Nagy", 
         "Title": "Senior Account Manager - OEM" 
        } 
       ], 
       "StartDate": "/Date(1338932700000+0000)/", 
       "Title": "Preparing for Windows 8" 
      } 
     ] 
    }, 
    { 
     "Contents": [ 
      { 
       "Id": 382, 
       "LastUpdated": "/Date(1338347251043+0000)/", 
       "Title": "HP EDW Appliance Solution Brief", 
       "Url": "http://infra2apps.blob.core.windows.net/eventapp/HP EDW Appliance Solution Brief_booth3.pdf" 
      }, 
      { 
       "Id": 380, 
       "LastUpdated": "/Date(1338347250970+0000)/", 
       "Title": "HP DBCA Datasheet", 
       "Url": "http://infra2apps.blob.core.windows.net/eventapp/11475_DBCA-Datasheet_booth3.pdf" 
      } 
     ], 
     "Id": 3, 
     "ImageUrl": "http://infra2apps.blob.core.windows.net/eventapp/SQL.png", 
     "Name": "Enterprise Information Platform", 
     "Sessions": [ 
      { 
       "Description": "The Database Consolidation appliance integrates software and harware into a turnkey solution that creates tremendous opportunities to virtualize demanding applications requiring enterprise-class resiliency. This session will dive into this NEW Private Cloud Appliance, leveraging virtualization and tuned and balanced infrastructure to deliver Private Cloud capabilities. We’ll go into detail including how to inventory and gather performance characteristics to provide detailed appliance-specific sizing and placement guidance using the MAP toolkit, how the appliance architecture enables high IO performance, isolation. and resiliency, and into details around core operational capabilities such as P-to-V and self-service workflow-enabled provisioning of new instances. Also, we will cover advanced capabilities such as chargeback, sustained engineering (upgrades and patching), load balancing with Live Demos that will show off the core capabilities of the appliance.", 
       "EndDate": "/Date(1339017300000+0000)/", 
       "FriendlyName": "TB3323", 
       "Id": 5, 
       "Location": "TBD", 
       "Speakers": [ 
        { 
         "Company": "Microsoft", 
         "Email": "[email protected]", 
         "Name": "Mark Mortimore", 
         "Title": "Senior Program Manager, SQL Server Appliances" 
        } 
       ], 
       "StartDate": "/Date(1339014600000+0000)/", 
       "Title": "Cloud on your terms - Database Consolidation Appliance" 
      }, 
      { 
       "Description": "Leveraging virtual and scalable infrastructure - 100s of servers can be efficiently, reliably, and manageably consolidated. This session will dive into the NEW DBC Appliance, leveraging virtualization and tuned and balanced infrastructure. Using demos, we will show all phases of consolidation including how to assess environments using the MAP toolkit, details around core operational capabilities such as P-to-V and self-service workflow-enabled provisioning of new fully configured servers. Also, we will cover advanced capabilities such as chargeback, s load balancing… ", 
       "EndDate": "/Date(1339006800000+0000)/", 
       "FriendlyName": "DT3324", 
       "Id": 6, 
       "Location": "TBD", 
       "Speakers": [ 
        { 
         "Company": "Microsoft", 
         "Email": "[email protected]", 
         "Name": "Mark Mortimore", 
         "Title": "Senior Program Manager, SQL Server Appliances" 
        } 
       ], 
       "StartDate": "/Date(1339005600000+0000)/", 
       "Title": "Database Consolidation & Private Cloud Appliance" 
      } 
     ] 
    }, 
    { 
     "Contents": [], 
     "Id": 4, 
     "ImageUrl": "http://infra2apps.blob.core.windows.net/eventapp/WinStorageSvr.png", 
     "Name": "Windows Storage Server", 
     "Sessions": [] 
    }, 
    { 
     "Contents": [], 
     "Id": 5, 
     "ImageUrl": "http://infra2apps.blob.core.windows.net/eventapp/SQL.png", 
     "Name": "Platform Modernization/Migration", 
     "Sessions": [ 
      { 
       "Description": "Microsoft and HP engineering have collaborated on delivering mission critical systems which outperform historic tier 1 platforms. Although many companies rely on the combination of HP and Microsoft for Mission Critical workloads, there has been a lingering doubt that the combined platform can scale to the needs of large enterprises currently relying on IBM Mainframes. This session will describe a signficant study just completed that puts to rest the myths that mainframes are the only platform that scales, can support high-IO and delivery mission critical capabilities. Finally an apples to apples comparison that you can use as you modernize your IT enviornment.", 
       "EndDate": "/Date(1339093200000+0000)/", 
       "FriendlyName": "DT3471", 
       "Id": 8, 
       "Location": "TBD", 
       "Speakers": [ 
        { 
         "Company": "Microsoft", 
         "Email": "[email protected]", 
         "Name": "Scott Rosenbloom", 
         "Title": "SSP Platform Modernization" 
        } 
       ], 
       "StartDate": "/Date(1339092000000+0000)/", 
       "Title": "Mainframe Alternative: Windows Server, SQL Server and HP DL 980" 
      } 
     ] 
    } 
] 
+0

@Logical - 주석에서 링크에 대한 인라인 구문 만 지원됩니다 :'[The title] (http://example.com/)' – vikingosegundo

답변

1

죄송합니다 아래의 코드를 설명하지 않는 때가 문제로

-(void) dataRecieved: (NSDictionary *) receivedDict 
{ 
NSArray *contentsArray = [receivedDict objectForKey:@"Contents"]; 
NSArray *sessionsArray = [receivedDict objectForKey:@"Sessions"]; 

for (NSDictionary *contentsDict in contentsArray) 
    { 
     Contents *objContents = [[Contents alloc]init]; 
     objContents.id = [contentsDict objectForKey:@"ID"]; 
     objContents.lastUpdate = [contentsDict objectForKey:@"LastUpdate"]; 
     objContents.title = [contentsDict objectForKey:@"Title"]; 

     [contentObjectsArray addObject: objContents]; 
    } 

    for (NSDictionary *sessionsDict in sessionsArray) 
    { 
     Sessions *objSessions = [[Sessions alloc]init]; 
     objSessions.id = [contentsDict objectForKey:@"ID"]; 
     objSessions.endDate = [contentsDict objectForKey:@"EndDate"]; 

     NSArray *speakersArray = [receivedDict objectForKey:@"Speakers"]; 

     for (NSDictionary *spearkersDict in speakersArray) 
     { 
      Speakers *objSpeakers = [[Speakers alloc]init]; 
      objSpeakers.title = [spearkersDict objectForKey:@"Title"]; 
      [objSessions.speakerssObjectsArray addObject: objSpeakers]; 
     } 

     [sessionsObjectsArray addObject:objSessions]; 
    } 
} 
3

귀하의 응답이 유효한 JSON 없습니다 : 여기

는 반응이다. 표준 JSON 구문 분석기를 사용하여 구문 분석 할 수 없습니다. (당신의 유효성을 확인하는 http://jsonlint.com/를 사용할 수 있습니다.)

// 편집 : 를 따라서 당신의 편집 후 유효한 JSON 데이터가 없습니다. 이를 파싱하려면 찾은 모든 JSON 구문 분석 프레임 워크 (예 : json-framework 등 인기있는 프레임 워크)를 사용할 수 있습니다.

iOS5 전용으로 제작하는 경우 시스템 라이브러리 NSJSONSerialization을 사용할 수 있습니다.

1

iOS 5 이상을 타겟팅하는 경우 JSON 지원이 기본이므로 외부 라이브러리를 추가하거나 파서를 구현할 필요가 없습니다. NSJSONSerialization을 사용하기 만하면된다.

See the documentation

예제 코드 : 당신은 아이폰 OS 버전을 대상으로하는 경우

NSError* error = nil; 

NSArray* parsed = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 

if(!error) { 
    //Process here 
} 

낮은는에서 iOS 5보다 다음 JSON 구문 분석은 기본적으로 지원되지 않으며, 당신은 파서를 직접 구현하거나, 사용자의 요구에 맞는 외부 라이브러리를 사용해야합니다 . 라이브러리의 예 : SBJSON

1

사용 SBJsonParser는 JSON을 구문 분석하고 당신은 당신이 알 수 있도록 JSON을 시각화하기 위해이 link을 사용할 수 있습니다 여기서 어떤 데이터 구조가 사용될 것이며 나머지는 @imsult가 말한 것과 같습니다.

관련 문제