1

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_Id = [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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Yes you are right but done like this NSArray *reviewsArray = [reviewsvalues valueForKey:@"Data"]; but ? how can i set the value on to the cell and how can count row
@Harish: reviewArray at each of its index contains a dictionary.
Can you come on line chat on stackchat
@Harish: I have edited my answer and given more code on how you need to do this. Hope this helps. If you still have doubts then please let me know
@Harish: Can you please wait for half an hour for participating in stackchat?
|
1

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.

5 Comments

Than how can i handle when something slightly unexpected happens.
even it not work it get crash and give error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]:
at this link NSArray *reviewsArray = [reviewsvalues objectAtIndex:@"Data"]; it show me warning that passing argument 1 of 'objectAtIndex'makes integer from pointer without a cast
@harish Sorry, my mistake, should be objectForKey: not objectAtIndex:.
@harish have a look at the headers for NSDictionary, and NSArray... try to keep them separate in your mind. Be aware that you will mix them up (as I just did). That's what compiler warnings are for ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.