2013-10-09 64 views
1

請幫我調試代碼無法識別的選擇發送到實例在JSON解析

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^{ 
    NSError *error = nil; 
    NSURL *urls = [NSURL URLWithString:[NSString stringWithFormat:@"http://cnapi.iconnectgroup.com/api/UserProfile?id=1"]]; 
    NSString *json = [NSString stringWithContentsOfURL:urls encoding:NSASCIIStringEncoding error:&error]; 
     NSLog(@"JSon data = %@ and Error = %@", json, error); 
    if(!error) 
    { 
     NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding]; 
     NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; 
     NSLog(@"JSON data is :: %@", myJsonArray); 

     for(NSDictionary *jsonDictionary in myJsonArray) 
     { 
      //NSString *uids = jsonDictionary[@"UID"]; 
      NSString *address1 = jsonDictionary[@"Address1"]; 
      NSString *address2 = jsonDictionary[@"Address2"]; 
      NSString *city = jsonDictionary[@"City"]; 
      NSString *emailId = jsonDictionary[@"EmailID"]; 
      NSString *fname = jsonDictionary[@"FName"]; 
      NSString *fax = jsonDictionary[@"Fax"]; 
      NSString *lname = jsonDictionary[@"LName"]; 
      NSString *password = jsonDictionary[@"Password"]; 
      NSString *phone = jsonDictionary[@"Phone"]; 
      NSString *state = jsonDictionary[@"State"]; 
      NSString *uids = [jsonDictionary objectForKey:@"UID"]; 
      NSString *zip = jsonDictionary[@"Zip"]; 
      NSString *company = jsonDictionary[@"company"]; 
      NSString *department = jsonDictionary[@"department"]; 

      NSLog(@"Uid is = %@", uids); 
      NSLog(@"First Name = %@", fname); 
      NSLog(@"Last Name = %@", lname); 
      NSLog(@"Company = %@", company); 
      NSLog(@"Email Id = %@", emailId); 
      NSLog(@"Password = %@", password); 
      NSLog(@"Department = %@", department); 
      NSLog(@"Address 1 = %@", address1); 
      NSLog(@"Address 2 = %@", address2); 
      NSLog(@"City = %@", city); 
      NSLog(@"State = %@", state); 
      NSLog(@"Zip = %@", zip); 
      NSLog(@"Phone = %@", phone); 
      NSLog(@"Fax = %@", fax); 

     } 
    } 

    }); 
    [activity stopAnimating]; 
    self.activity.hidden = YES; 
} 

圖片會給你錯誤所在。點擊stepover進行調試後出現此錯誤。我也試過

NSString *address1 = [jsonDictionary objectForKey:@"Address1"]; 

enter image description here

+1

當你記錄myJsonArray的內容時,你在控制檯上看到了什麼? –

+0

你確定jsonDictionary是一個NSDictionary,我認爲它是一個字符串 –

+0

顯示你的myJsonArray – wesley

回答

2

從url的輸出中,它顯示它不是數組而是字典。你正試圖在這裏轉換爲數組。

NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; 

改用此

NSDictionary *myJsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; 

然後刪除for循環不需要它。並用myJsonDictionary替換你的變量jsonDictionary,以便檢索值。

// for(NSDictionary *jsonDictionary in myJsonArray) 

現在運行它會沒事的。爲我工作得很好

如果輸出是字典數組,它會看起來像這樣用方括號括起來。

For Ex: [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}] 

如果你不知道從URL響應的性質,你可以檢查 類型。對於前:

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; 

if ([jsonObject isKindOfClass:[NSArray class]]) { 
    NSLog(@"its an array!"); 

    NSArray *myJsonArray = (NSArray *)jsonObject; 
    // Handle Array of Dictionary 

for(NSDictionary *jsonDictionary in myJsonArray) 
    { 
     NSString *address1 = jsonDictionary[@"Address1"]; 
     //and so on 
    } 
} 
else { 
NSLog(@"It's Dictionary"); 
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject; 
NSLog(@"jsonDictionary - %@",jsonDictionary); 
//Handle NSDictionary 
} 
1

jsonDictonary是一個的NSString不像你想象NSDictonary。

仔細檢查你的JSON,也許在調用該函數之前檢查它是否是NSDictonary。

+0

如果我用NSString代替NSDicationary for lop,我得到了多個編譯時錯誤,指出「期望的方法讀取字典元素在'NSString *'類型的對象上找不到' –

+0

@DeepakThakur - 實際上*在你的JSON和*理解*它的意思,然後塑造代碼來匹配。不要從其他地方複製代碼,只是試圖破解它,直到它工作。 JSON非常簡單*如果你只是嘗試首先理解它。請參閱json.org。 –

0

你應該使用半標準框架的一個解析JSON到核心數據。有一些關於這些問題的SO問題。

在這種情況下,您的JSON只有一個不是數組的對象。 一般而言,如果服務器發送了意外事件,應用程序不應該中止,因此最好使用解析器,它將大聲抱怨格式錯誤的JSON,而不是從頭開始。

相關問題