开发者

Getting the error in JSON parsing in iphone

开发者 https://www.devze.com 2023-04-11 08:04 出处:网络
I am trying to pars the json data and display in table My JSON data is like this {\"isError\":false,\"ErrorMessage\":\"\",\"Result\":{\"Count\":4,\"Data\":[{\"ContentID\":\"127_30_1309793318065\",\

I am trying to pars the json data and display in table My JSON data is like this

{"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;}

I am prasing like this

-(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_I开发者_如何学JAVAd = [value objectForKey:@"DateRecorded"];
        result.Average_Rating = [value objectForKey:@"AverageRating"];
        //OVER here MY APP GET CRASH 


    }


} 

BUt it get crash and give error

[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance


The problem is simple.

reviewsvalues should be an NSDictionary and you should not be calling objectAtIndex: for the reviewsvalues.

Instead you should call valueForKey like

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

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

Hope this helps you.

Please let me know if you want more help on this.


You set reviewsvalues = [result objectForKey:@"Result"];

Which means reviewsvalues is now an NSDictionary. "Result" is a dictionary, not an array:

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

NSDictionary doesn't respond to -objectAtIndex:, that's one of NSArray's methods.

You need another step:

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

and while you are at it, you can use fast enumeration.

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"];
}

Edit: also, you should know that you've not coded this very defensively. What happens if the data isn't exactly as it is in your example? what happens if a value is missing, like "Data", or "Result"? Your app should be robust enough to not choke if something slightly unexpected happens.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号