0

I am fetching data from server using json. It is checked url is getting hit response is received, printed in console all values fetched from json. But dictionary and tried with array also both are showing null values by breakpoints but when printed in console showing data is fetched. Below is the code.

NSString *urlStr = [NSString stringWithFormat:@"http://server39.pivbfg.com/360ads/apps/ads/%@/android/1360/ord0.9109502528132325?json=1&package_url=%@",self.mStrPid, base64String];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                            [NSURL URLWithString:
                             [urlStr stringByAddingPercentEscapesUsingEncoding:
                              NSUTF8StringEncoding]]];
NSLog(@"req-------------%@",request);

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

NSDictionary *json_dict = [json_string JSONValue];
NSLog(@"json_dict\n%@",json_dict);
NSLog(@"json_string\n%@",json_string);
NSMutableArray *arrAds = [[NSMutableArray alloc]init];
arrAds = [json_dict valueForKey:@"ads"];

request is going ok. json_string is ok. But json_dict printing values in console but showing null at breakpoints. What could be the reason for this. One thing is i am using ARC, does it affect this code. Please guide for the above.

This is the error: * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x9851e00> valueForUndefinedKey:]: this class is not key value coding-compliant for the key ads.'* First throw call stack: (0xb5012 0x13a9e7e 0x13dfb1 0xe565ed 0xdc28db 0xdc288d 0x613d 0x3d1707 0x3d1772 0x320915 0x320caf 0x320e45 0x329e57 0x5942 0x2ed697 0x2edc87 0x2eee8b 0x3001f5 0x30112b 0x2f2bd8 0x2202df9 0x2202ad0 0x2abf5 0x2a962 0x5bbb6 0x5af44 0x5ae1b 0x2ee6ba 0x2f053c 0x544d 0x2b65 0x1) libc++abi.dylib: terminate called throwing an exception

Edited code

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSError *error;
    NSData *jsonData = [json_string dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSLog(@"results\n%@",results);
NSMutableArray *arrAds = [[NSMutableArray alloc]init];
arrAds = [results valueForKey:@"ads"];
NSLog(@"dict-------------%@",arrAds);

Now the same problem is coming with the array arrAds. It is printing values in console but empty.

iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54

1 Answers1

0

Well the error tells you that the property you are retrieving from the JSON is not a dictionary but a string. It looks like you json_string does not contain a valid JSON object.

In your example you'r also leaking:

NSMutableArray *arrAds = [[NSMutableArray alloc]init];
arrAds = [json_dict valueForKey:@"ads"];

You create a new NSMutableArray only to assing a new object to the same variable the next line. Also the object returned will not a none mutable version. You can just replace it with:

NSMutableArray *arrAds = [[json_dict objectForKey:@"ads"] mutableCopy];  
rckoenes
  • 69,092
  • 8
  • 134
  • 166