2011-10-05 48 views
1

解析我試圖收杆JSON數據並顯示在表 我的JSON數據是這樣的收到錯誤的JSON在iphone

{"isError":false,"ErrorMessage":"","Result":{"Count":4,"Data":[{"ContentID":"127_30_1309793318065","ContentTypeID":1,"UserCaption":"Gandhinagar(Kanjurmarg)","UserComment":"central\n","DateRecorded":"\/Date(1309793318000+0530)\/","Data":"","ShareType":true,"Views":0,"PlayTime},{},{},{}];};isError = 0;} 

我prasing這樣

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{  
    NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",loginStatus); 



    //this is for the getting the data from the server with help of JSON 
    NSString *json_string = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 
    NSDictionary *result = [json_string JSONValue]; 
    /
    //this for holding the Array value which come from the server 
    NSMutableArray *results = [[NSMutableArray alloc] init]; 
    for (int index = 0; index<[reviewsvalues count]; index++) 
    { 
     NSMutableDictionary * value = [reviewsvalues objectAtIndex:index]; 
     ReviewsResult * result = [[ReviewsResult alloc] init]; 
     result.User_Caption = [value objectForKey:@"UserCaption"]; 
     result.ContentType_Id = [value objectForKey:@"DateRecorded"]; 
     result.Average_Rating = [value objectForKey:@"AverageRating"]; 
     //OVER here MY APP GET CRASH 


    } 


} 

但它會崩潰並給出錯誤

[__NSCFDictionary objectAtIndex:]:無法識別的選擇器發送到實例

回答

2

問題很簡單。

reviewsvalues應該是一個NSDictionary,你不應該爲reviewsvalues調用objectAtIndex:

相反,你應該叫valueForKey

int count = [[reviewsvalues valueForKey:@"Count"] intValue]; 
NSArray *reviewsArray = [reviewsvalues valueForKey:@"Data"]; 
int count = [reviewsArray count]; 

cell.textLabel.text = [[reviewsArray objectAtIndex:indexPath.row] valueForKey:@"ContentID"]; 

希望這有助於你。

請讓我知道你是否想要更多的幫助。

+0

是的,你是對的,但像這樣做NSArray * reviewsArray = [評論值valueForKey:@「數據」];但?我該如何設置單元格的值以及如何計算行 – Harish

+0

@Harish:reviewArray在其每個索引中都包含一個字典。 –

+0

你可以來stackchat上的在線聊天 – Harish

1

您設置reviewsvalues = [result objectForKey:@"Result"];

這意味着現在reviewsvaluesNSDictionary。 「結果」是一個字典,不是數組:

{"Count":4,"Data":[...]} 

NSDictionary並不-objectAtIndex:響應,這是的NSArray的方法之一。

你需要一個步驟:

NSArray *reviewsArray = [reviewsvalues objectForKey:@"Data"]; 

和,而你在它,你可以使用快速枚舉。

for (NSDictionary *review in reviewsArray) { 
    ReviewsResult * result = [[ReviewsResult alloc] init]; 
    result.User_Caption = [review objectForKey:@"UserCaption"]; 
    result.ContentType_Id = [review objectForKey:@"DateRecorded"]; 
    result.Average_Rating = [review objectForKey:@"AverageRating"]; 
} 

編輯:另外,你應該知道你沒有非常防守地編碼。如果數據與您的示例中的數據不完全相同,會發生什麼情況?如果缺少一個值,如「數據」或「結果」,會發生什麼情況? 您的應用程序應該足夠強大,以防止發生輕微意外事件時不會窒息。

+0

比稍微意外的事情發生時我該如何處理。 – Harish

+0

即使它不工作,它會崩潰並給出錯誤終止應用程序由於未捕獲異常'NSInvalidArgumentException',原因:' - [__ NSCFDictionary objectAtIndex:]: – Harish

+0

在此鏈接NSArray * reviewsArray = [評論值objectAtIndex:@「Data」];它顯示我警告,通過參數1'objectAtIndex'從指針變爲無整型轉換的整數 – Harish