ios – NSDictionary是零

ios – NSDictionary是零,第1张

概述我想解析一个本地json,我试图这样做,但testDict是零.有人可以帮我吗? - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{ [self.searchResult removeAllObjects]; NSPredicate *resultPredicat 我想解析一个本地Json,我试图这样做,但testDict是零.有人可以帮我吗?

- (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,"Cab": [  "(n.) A kind of close carriage with two or four wheels,containing a little over two (2.37) pints."],

我收到了这个错误:

无法读取数据,因为它的格式不正确.“(JsON文本不是以数组或对象开头,而是选项允许未设置片段

事实证明你的JsON格式不好.要修复你的Json,请在文件中添加一个{在Json的开头,在最后添加一个}.
您可以通过检查this site来检查您的JsON是否格式正确.如果您的数据无效,您的NSDIctionary将为null.

将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,containing a little over two (2.37) pints."    );}
总结

以上是内存溢出为你收集整理的ios – NSDictionary是零全部内容,希望文章能够帮你解决ios – NSDictionary是零所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1026997.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存