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 http://eandata.com/blog/e53/data-feed-v3---access-and-data-layout/ 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 http://eandata.com/feed/?v=3&keycode=027F74ED52886617&mode=json&find=883929215010&get=all 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)
break;
NSString *scannedNumber = symbol.data;//@"0049000006582";
NSString *name = symbol.typeName;
NSLog(@"The type name is %@", name);
NSString *eanDataExample =
@"http://eandata.com/feed/?v=3&keycode=027F74ED52886617&mode=json&find=";
[ 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