2012-03-20 84 views
0

我有這個代碼就在這裏一個問題:的Xcode JSON麻煩

- (void)fetchedData:(NSData *)responseData { 
    //parse out the json data 
    NSError* error; 
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

    NSArray* announcements = [json objectForKey:@"clients"]; 

    NSLog(@"laag 1: %@", announcements); 

    NSDictionary* announcement = [announcements objectAtIndex:0]; 

    NSNumber *status = [announcement objectForKey:@"status"]; 

    humanReadble.text = [NSString stringWithFormat:@"%@", status]; 
} 

我試圖從this網址提取JSON數據。我知道抓取工作,NSLog顯示它很好,但問題始於NSDictionary。我用斷點做了一些測試,發現錯誤在那裏。這是我的日誌的概述:

2012-03-20 23:15:13.210 Json test 2[13248:f803] laag 1: { 
    client =  (
       { 
      companyname = xxx; 
      datecreated = "2012-03-11"; 
      email = "xxxx"; 
      firstname = xxx; 
      groupid = 0; 
      id = 3; 
      lastname = O; 
      status = Active; 
     }, 
       { 
      companyname = "xxx"; 
      datecreated = "2012-03-02"; 
      email = "xxx"; 
      firstname = xxx; 
      groupid = 0; 
      id = 1; 
      lastname = "xxx"; 
      status = Active; 
     }, 
       { 
      companyname = ""; 
      datecreated = "2012-03-08"; 
      email = "xxx"; 
      firstname = xxx; 
      groupid = 0; 
      id = 2; 
      lastname = xxx; 
      status = Active; 
     }, 
       { 
      companyname = xxx; 
      datecreated = "2012-03-13"; 
      email = "xxx"; 
      firstname = xxx; 
      groupid = 0; 
      id = 4; 
      lastname = "xxx"; 
      status = Active; 
     } 
    ); 
} 
2012-03-20 23:15:13.212 Json test 2[13248:f803] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6821f30 
2012-03-20 23:15:13.213 Json test 2[13248:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6821f30' 
*** First throw call stack: 
(0x13c7022 0x1558cd6 0x13c8cbd 0x132ded0 0x132dcb2 0x21a2 0x13c8e42 0x9379df 0x139b94f 0x12feb43 0x12fe424 0x12fdd84 0x12fdc9b 0x12b07d8 0x12b088a 0x11626 0x1c3d 0x1ba5) 
terminate called throwing an exception(lldb) 

有人可以幫我嗎?

我使用獅子SDK 5.1,4.3的xcode 10.7.3

回答

0

通過[json objectForKey:@"clients"]在代碼返回的對象實際上是一個字典,而不是陣列。首先,您需要從該字典中提取「客戶端」對象(這是您想要的數組)。你可以這樣做:

NSDictionary *clients = [json objectForKey:@"clients"]; 
NSArray *announcements = [clients objectForKey:@"client"]; 
NSDictionary* announcement = [announcements objectAtIndex:0]; 
+0

Tnx很糊塗!像魅力一樣工作! – Roeliee 2012-03-20 22:38:04