我从未做过任何类型的Web编程,但我需要请求一个JSON对象并以objective-c形式保存数据。我正在使用http://eandata.com/blog/e53/data-feed-v3---access-and-data-layout/
处的文档,但我无法检索与匹配键相关联的任何字符串。我试图解析的示例JSON对象是http://eandata.com/feed/?v=3&keycode=027F74ED52886617&mode=json&find=883929215010&get=all
顶级JSON对象返回三个键;地位,产品和公司。所以我访问产品密钥并将其分配给NSDictionary。在那个字典中是另一个具有关键属性的NSDictionary,所以我拿出那个字典并分配它。在属性字典内部是一个具有字符串值的关键产品。这是我需要获取的字符串,但由于某种原因,它打印为空字符串。这是相关的代码。
扫描条形码并启动GET请求
#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];
}
以下是URLConnection委托方法
#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);
}
}
}
相关日志声明。我只包括那些与我想要访问的领域相关的内容。
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
答案 0 :(得分:3)
当我尝试使用您的代码时,插入您在问题中发布的完整网址,它运行正常。您应该记录url
变量,以查看您实际传递给您的请求的内容。看起来您实际上从未实际附加扫描的数字,因为这一行:
[ eanDataExample stringByAppendingString:scannedNumber ];
您没有将该表达式的值赋给任何东西。它应该是:
eanDataExample = [eanDataExample stringByAppendingString:scannedNumber];