JSON 对象为具有值的键返回空字符串

Posted

技术标签:

【中文标题】JSON 对象为具有值的键返回空字符串【英文标题】:JSON object returning empty string for key that has a value 【发布时间】:2013-11-17 18:42:54 【问题描述】:

我从未做过任何类型的网络编程,但我需要请求一个 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 

【问题讨论】:

【参考方案1】:

当我尝试您的代码并插入您在问题中发布的完整网址时,它运行良好。您应该记录您的 url 变量以查看您实际传递给您的请求的内容。由于这一行,您似乎从未真正附加扫描的数字:

[ eanDataExample stringByAppendingString:scannedNumber ];

您不会将该表达式的值分配给任何东西。应该是:

eanDataExample = [eanDataExample stringByAppendingString:scannedNumber];

【讨论】:

以前从未做过这种事情,所以我认为我在理解数据访问方面做错了。事实证明,即使字段为空白,URL 中传递的 all 参数也会返回这些键。

以上是关于JSON 对象为具有值的键返回空字符串的主要内容,如果未能解决你的问题,请参考以下文章

json字符串转对象,遇到空字符串会抛异常吗

为啥 json_encode 会返回一个空字符串

如何使用 Access VBA 将具有默认值的未绑定文本框的值设置为空字符串

105 - 获取模型列表

java中json对象有没有方法可以把null值转换成空字符串

java中json对象有没有方法可以把null值转换成空字符串