Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Ios 这本字典是零_Ios_Objective C_Json_Nsdictionary - Fatal编程技术网

Ios 这本字典是零

Ios 这本字典是零,ios,objective-c,json,nsdictionary,Ios,Objective C,Json,Nsdictionary,我想解析一个本地json,我试着这样做,但是testDict是nil。有人能帮我吗 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { [self.searchResult removeAllObjects]; NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF conta

我想解析一个本地json,我试着这样做,但是
testDict
是nil。有人能帮我吗

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [self.searchResult removeAllObjects];
        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    NSString * firstLetter = [searchText substringWithRange:[searchText rangeOfComposedCharacterSequenceAtIndex:0]];

    NSError *err = nil;
    NSString *aux=@"english_";
    NSString *updated = [aux stringByAppendingString:firstLetter];

    NSString *dataPath = [[NSBundle mainBundle] pathForResource:updated ofType:@"json"];

    testDict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];
Json看起来像这样:

"Caaba": "(n.) The small and nearly cubical stone building, toward which all Mohammedans must pray.",
   "Caas": "(n. sing. & pl.) Case.",
   "Cab": [
      "(n.) A kind of close carriage with two or four wheels, usually a public vehicle.",
      "(n.) The covered part of a locomotive, in which the engineer has his station.",
      "(n.) A Hebrew dry measure, containing a little over two (2.37) pints."
   ],

我正在检查是否是验证json,我使用了以下代码:

NSString *path = @"/Users/JohnApple/Desktop/myJsonFileForThisTest.json";
NSError *err;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:kNilOptions error:&err];
NSLog(@"Here is the NSDicitonary if this worked --> %@",dict);
NSLog(@"Here is the NSError if it failed --> %@", err);
当我使用您在上面提供的Json数据时:

"Caaba": "(n.) The small and nearly cubical stone building, toward which all Mohammedans must pray.",
"Caas": "(n. sing. & pl.) Case.",
"Cab": [
  "(n.) A kind of close carriage with two or four wheels, usually a public vehicle.",
  "(n.) The covered part of a locomotive, in which the engineer has his station.",
  "(n.) A Hebrew dry measure, containing a little over two (2.37) pints."
],
我得到了这个错误:

无法读取数据,因为它的格式不正确。”(JSON文本没有以数组或对象开头,并且没有设置允许片段的选项

原来您的JSON格式不正确。若要修复JSON,请在文件中JSON的开头添加一个
{
,并在结尾添加一个
}
。 您可以通过检查查看JSON的格式是否正确。如果数据无效,则NSDIctionary将为空

将Json设置为正确格式后,以下数据显示正确:

{
    Caaba = "(n.) The small and nearly cubical stone building, toward which all Mohammedans must pray.";
    Caas = "(n. sing. & pl.) Case.";
    Cab =     (
        "(n.) A kind of close carriage with two or four wheels, usually a public vehicle.",
        "(n.) The covered part of a locomotive, in which the engineer has his station.",
        "(n.) A Hebrew dry measure, containing a little over two (2.37) pints."
    );
}

你能发布你试图解析的JSON吗?
err
告诉你什么?对我来说,这看起来根本不是一个有效的JSON。它不是有效的JSON:首先在这里检查你的JSON好吧,我猜JSON会返回一个字典,但你正在尝试使用一个
NSDictionary
。尝试对变量使用
NSArray
de>testDict