0

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?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
SampathKumar
  • 2,525
  • 8
  • 47
  • 82
  • From the error it seems dic3 is an array and not a dictionary. Can you NSLog the class of dic3 and check. – Rushi Jan 28 '13 at 07:27
  • Look at your JSON - the "items" key is associated with an array, not a dictionary. – Carl Veazey Jan 28 '13 at 07:27
  • This isn't related to your problem, but please remove all those unsightly casts. They are usually not necessary in Objective-C and often just hide errors. – Stig Brautaset Jan 28 '13 at 11:11

4 Answers4

2

This is the problem:

 NSDictionary *dic4 = (NSDictionary *) [dic3 objectForKey:@"userid"];

You should use:

NSDictionary *dic4 = (NSDictionary *) [[dic3 objectAtIndex:0] objectForKey:@"userid"];
Ivan Alek
  • 1,899
  • 3
  • 19
  • 38
  • The real problem is `NSDictionary *dic3 = (NSDictionary *) [dic1 objectForKey:@"items"];` since the "items" key is mapped to an array. It should be `NSArray *items = (NSArray *)[dic1 objectForKey:@"items"];`. – Carl Veazey Jan 28 '13 at 07:31
  • i tried this but the error comes in "No visible @interface for nsdictionary declare the selector objectatindex" – SampathKumar Jan 28 '13 at 07:55
  • Change this: NSDictionary *dic3 = (NSDictionary *) [dic1 objectForKey:@"items"]; into: NSArray *dic3 = (NSArray *) [dic1 objectForKey:@"items"]; – Ivan Alek Jan 28 '13 at 07:59
0
-(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"];
NSArray *arr3 = (NSArray *) [dic1 objectForKey:@"items"];

NSString *str = [[arr objectAtIndex:0]objectForKey:@"userid"];
NSlog(@"userid ==%@",str);
// NSDictionary *dic4 = (NSDictionary *) [dic3 objectForKey:@"userid"]; // App crash in this line
 }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Aman Aggarwal
  • 3,754
  • 1
  • 19
  • 26
0

You can add SBJSON framework files. and By parsing the by

NSDictionary *responseDict = [response JSONValue];
just parse it as normal dictionary.

NSString *userIDValue=[NSString stringWithFormat:@"%@",[[[[responseDict valueForKey:@"response"]valueForKey:@"items"]objectAtIndex:0]valueForKey:@"userid"]];
Nimantha
  • 6,405
  • 6
  • 28
  • 69
swapnil
  • 25
  • 2
-1
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"]);
 }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
SampathKumar
  • 2,525
  • 8
  • 47
  • 82