【发布时间】: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