iphoneiosjsonparsingsbjson

Issue in JSON parser in iPhone?


JSON Response like - {"response":{"Success":"Y","items":[{"userid":"255"}]}}

I tried to parse like this:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *jsonStr = [[NSString alloc] initWithData:mutaebleData encoding:NSUTF8StringEncoding];
    NSLog(@"JSonSTr : %@", jsonStr);
    
    SBJSON *json = [[SBJSON alloc]init];
    
    NSDictionary *dic = (NSDictionary *) [json objectWithString:jsonStr];
    NSDictionary *dic1 = (NSDictionary *) [dic objectForKey:@"response"];
    NSDictionary *dic2 = (NSDictionary *) [dic1 objectForKey:@"Success"];
    NSDictionary *dic3 = (NSDictionary *) [dic1 objectForKey:@"items"];

    NSDictionary *dic4 = (NSDictionary *) [dic3 objectForKey:@"userid"]; // App crash in this line
}

How to get userid value?


Solution

  • NSDictionary *dic = (NSDictionary *) [json objectWithString:jsonStr];
     NSDictionary *dic1 = (NSDictionary *) [dic objectForKey:@"response"];
     NSDictionary *dic2 = (NSDictionary *) [dic1 objectForKey:@"Success"];
     NSDictionary *dic3 = (NSDictionary *) [dic1 objectForKey:@"items"];
        
     for(NSDictionary *str in dic3)
     {
        NSLog(@"str:%@",[str valueForKey:@"userid"]);
     }