objective-c在项目中使用SBjson read file.json,但NSArray返回null
th.json ViewController.mobjective-c在项目中使用SBjson read file.json,但NSArray返回null,json,null,nsarray,internal,sbjson,Json,Null,Nsarray,Internal,Sbjson,th.json ViewController.m {"lessons":[{"id":"38","fach":"D"},{"id":"39","fach":"M"}]} 输出NSLog列表返回 NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSString *JsonData = [bundle pathForResource:@"th" ofType:@"json"]; SBJsonParser *parser = [
{"lessons":[{"id":"38","fach":"D"},{"id":"39","fach":"M"}]}
输出NSLog列表返回
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *JsonData = [bundle pathForResource:@"th" ofType:@"json"];
SBJsonParser *parser = [[SBJsonParser alloc] init] ;
NSDictionary *jsonObject = [parser objectWithString:JsonData error:NULL];
NSArray *list = [jsonObject objectForKey:@"lessons"];
NSLog(@"%@", list);
我的应用程序在我的项目内部使用json文件。
和我的项目文件夹中的“th.json”。
但是NSArray*列表不能在Json文件中显示数据
(null)
插入字符串使其工作。您需要将文件内容提供给SBJsonParser。您的代码只是将文件的路径提供给解析器。请尝试以下方法:
for (NSDictionary *dataDict in myitems) {
NSString *itemName = [dataDict objectForKey:@"itemName"];
NSLog(@"itemName = %@",itemName);
}
为什么生成错误:“SBJson4Parser”没有可见的@interface声明选择器“objectWithData:”SBJson4Parser来自SBJson版本4及更高版本,SBJsonParser(在本例中使用)位于小于4的SBJson版本中。这是一个重大的版本更改,因为API发生了重大更改。因此,所有内容都已重命名,因此您可以在同一应用程序中同时使用版本3和版本4。
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"th" ofType:@"json"];
NSLog(@"Path: %@", path);
NSData *jsonData = [NSData dataWithContentsOfFile:path];
NSLog(@"Data: %@", jsonData);
SBJsonParser *parser = [[SBJsonParser alloc] init] ;
NSDictionary *jsonObject = [parser objectWithData:jsonData];
NSLog(@"%@", jsonObject);
NSArray *list = [jsonObject objectForKey:@"lessons"];
NSLog(@"%@", list);