Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
objective-c在项目中使用SBjson read file.json,但NSArray返回null_Json_Null_Nsarray_Internal_Sbjson - Fatal编程技术网

objective-c在项目中使用SBjson read file.json,但NSArray返回null

objective-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 = [

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 = [[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);