【发布时间】:2011-11-05 20:52:38
【问题描述】:
我有一个文件:
{
"MapName" : "This is map1";
}
我尝试阅读的内容:
- (id)initFromFile:(NSString *)mapName
{
self = [super init];
if (self) {
NSString* path = [[NSBundle mainBundle] pathForResource:mapName ofType:@"json"];
NSData* jsonData = [NSData dataWithContentsOfFile:path];
assert(jsonData);
JSONDecoder* decoder = [[JSONDecoder alloc]
initWithParseOptions:JKParseOptionNone];
assert(decoder);
NSDictionary* json = (NSDictionary*)[decoder objectWithData:jsonData];
assert(json);
NSString* mapName = (NSString*)[json objectForKey:@"MapName"];
assert(mapName);
printf("MapName: %s\n", [mapName UTF8String]);
}
return self;
}
在 assert(json) 处失败; 有什么明显的我做错了吗?
我知道文件读取正常,但解码传回 NULL。
谢谢
【问题讨论】:
-
Eugene 非常接近:{ "MapName" : "This is map1" } 最后一项没有分隔符。逗号应该用在所有前面。
标签: objective-c json jsonkit