【问题标题】:Passing data from local file using json使用json从本地文件传递数据
【发布时间】:2012-06-02 22:26:08
【问题描述】:

我正在尝试将数据从我的 JSON 文件传递​​到一个简单的 ViewController 中的标签,但我不知道在哪里实际传递该数据。我可以只添加到我的setDataToJson 方法中,还是将数据添加到我的viewDidLoad 方法中?

这是我的代码

@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation;
@end

@implementation NSDictionary(JSONCategories)

+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{
    NSData* data = [NSData dataWithContentsOfFile:fileLocation];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data 
                                                options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}
@end

@implementation ViewController
@synthesize name;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)setDataToJson{

    NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"];
    name.text = [infomation objectForKey:@"AnimalName"];//does not pass data
}

【问题讨论】:

    标签: ios json nsdictionary


    【解决方案1】:

    问题在于您尝试检索文件的方式。为了做到这一点,您应该首先在包中找到它的路径。试试这样的:

    +(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{
        NSString *filePath = [[NSBundle mainBundle] pathForResource:[fileLocation stringByDeletingPathExtension] ofType:[fileLocation pathExtension]];
        NSData* data = [NSData dataWithContentsOfFile:filePath];
        __autoreleasing NSError* error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data 
                                                    options:kNilOptions error:&error];
        // Be careful here. You add this as a category to NSDictionary
        // but you get an id back, which means that result
        // might be an NSArray as well!
        if (error != nil) return nil;
        return result;
    }
    

    完成此操作并加载视图后,您应该能够通过检索 json 来设置标签,如下所示:

    -(void)setDataToJson{
        NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"];
        self.name.text = [infomation objectForKey:@"AnimalName"];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self setDataToJson];
    }
    

    【讨论】:

    • 如果我想从 Json 文件 self.lat.text = [[infomation valueForKey:@"Location"] valueForKey:@"Latitude"]; 中的一组数据中获取信息,我会更改我的 nsdictionary 函数吗?这就是我所拥有的,这就是 JSON 的样子 "Location": [ { "Longitude": 0, "Latitude": 0 } ], 以及何时我运行它说-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance
    • 那么你应该这样做:self.lat.text = [[[information valueForKey:@"Location"] objectAtIndex:0] valueForKey:@"Latitude"];
    • 好的,我试过了,现在它显示-[__NSCFNumber isEqualToString:] 而不是 NSArray。嗯
    • 啊,当然是数字了,我们得先转换一下。试试这个:self.lat.text = [[[[information valueForKey:@"Location"] objectAtIndex:0] valueForKey:@"Latitude"] stringValue];
    • 请注意,检查引用返回的错误实例是不安全的;相反,检查方法的返回值。来自 Apple 的错误处理编程指南:“虽然间接返回 Cocoa 错误域中的错误对象的 Cocoa 方法如果通过直接返回 nilNO 指示失败,则保证返回此类对象,在尝试对 NSError 对象执行任何操作之前,您应该始终检查返回值是否为 nilNO。"
    【解决方案2】:

    应该改为valueForKey

    例子:

    name.text = [infomation valueForKey:@"AnimalName"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 2023-02-19
      • 2018-05-11
      • 2020-03-24
      • 2013-08-28
      • 2017-03-30
      • 1970-01-01
      相关资源
      最近更新 更多