user2138602 user2138602 - 1 year ago 60
iOS Question

JSON object returning empty string for key that has a value

I have never done any kind of web programming but I need to request a JSON object and save the data in objective-c form. I am using the documentation at
but I am unable to retrieve any of the strings associated with a matching key. An example JSON object I am trying to parse is
The top level JSON object returns three keys; status, product, and company. So I access the product key and assign it to an NSDictionary. In that dictionary is another NSDictionary with the key attributes, so I pull out that dictionary and assign it. Inside the attributes dictionary is a key product with the value of a string. This is the string I need to get, but for some reason it is printing as the empty string. Here is the relevant code.

Scanning the barcode and initiation of GET request

#pragma ZBarDelegate
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
// ADD: get the decode results
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)

NSString *scannedNumber =;//@"0049000006582";
NSString *name = symbol.typeName;
NSLog(@"The type name is %@", name);

NSString *eanDataExample =
[ eanDataExample stringByAppendingString:scannedNumber ];
eanDataExample =
[ eanDataExample stringByAppendingString:@"&get=all" ];

NSURL *url = [ NSURL URLWithString:eanDataExample ];
NSURLRequest *request =
[ NSURLRequest requestWithURL:url ];
connection = [ NSURLConnection connectionWithRequest:request delegate:self ];
if ( connection ) {
webData = [ [ NSMutableData alloc ] init];
/*resultImage.image =
[info objectForKey: UIImagePickerControllerOriginalImage];*/

// ADD: dismiss the controller (NB dismiss from the *reader*!)
[reader dismissModalViewControllerAnimated: YES];

Here are the URLConnection delegate methods

#pragma NSURLConnectionDataDelegate Methods
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
[webData setLength:0 ];

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[ webData appendData:data ];

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSDictionary *allDataDictionary =
[ NSJSONSerialization JSONObjectWithData:webData options:0 error:nil ];
NSArray *keys = [ allDataDictionary allKeys ];
NSLog(@"The count of all keys is %d", keys.count);
NSDictionary *product = [ allDataDictionary objectForKey:@"product"];
NSDictionary *attributes = [ product objectForKey:@"attributes"];
NSLog(@"The number of attributes are %d", attributes.count);
NSArray *attKeys = [ attributes allKeys ];
NSString *desc;
for (NSString *s in attKeys) {
if ( [ [ attributes objectForKey:s ] isKindOfClass:[NSString class ] ] ) {
NSLog(@"The %@ object is a NSSTring", s);
NSLog(@"The att key is %@", s );
if ( [ s isEqualToString:@"product"] ) {
desc =
[ attributes objectForKey:@"product" ];
NSLog(@"The description is %@", desc);


Relevant log statements. I have only included those related to the field I am trying to access.

2013-11-17 11:25:21.254 Collections[860:60b] The product object is a NSSTring
2013-11-17 11:25:21.255 Collections[860:60b] The att key is product
2013-11-17 11:25:21.256 Collections[860:60b] The description is

Answer Source

When I tried your code, plugging in the complete url that you posted in your question, it worked fine. You should log your url variable to see what you're actually passing to your request. It looks like you never actually append the scanned number, because of this line:

[ eanDataExample stringByAppendingString:scannedNumber ];

You don't assign the value of that expression is to anything. It should be:

eanDataExample = [eanDataExample stringByAppendingString:scannedNumber];