【问题标题】:I use JSON but there is exception [NSString objectForKey:]我使用 JSON 但有异常 [NSString objectForKey:]
【发布时间】:2011-12-16 15:55:27
【问题描述】:

我在我的数据库中读取信息我使用 JSON 但有异常

'NSInvalidArgumentException',原因:'-[NSCFString objectForKey:]:无法识别的选择器发送到实例 0x603dfb0'

我认为这部分:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];

    NSArray *array = [str JSONValue];
    if (!array)
         return;

    NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
    [fmt setDateFormat:@"yyyy-MM-dd"];

    for (NSDictionary *dict in array) {

         NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
         NSLog(@"%@",d);
         [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
    }
}

【问题讨论】:

  • 可能需要查看 json 字符串(或一个接近的示例)才能看到它的样子。另外尝试 valueForKey 而不是 objectForKey 作为测试(我认为它们的行为不同,但想看看)。
  • 从另一个帖子中添加了一行,在我的回答中看起来相似。希望对您有所帮助。
  • 错误消息说你正在发送objectForKey,这是 NSDictionary 上的一个方法到一个 NSString。看起来你的类型不是你想象的那样。

标签: json nsstring nsdictionary


【解决方案1】:

“向链接器传递一个特殊标志,‘-ObjC’。调整我的项目的构建设置以将此标志包含在‘其他链接器标志’中”

根据:

http://beatworm.co.uk/blog/computers/using-categories-on-objective-c-classes-in-a-static-library/

编辑: Why it is terminating?

编辑2: 好的,不完全确定(因为我从来没有做过这种类型的编码),但是给定 json 数据:

{"event":[
  {"eve_date":"2011-12-18","eve_time":"00:00:06","title_event":"Google event","description":"Google event for IT student"},
  {"eve_date":"2011-12-29","eve_time":"00:00:08","title_event":"microsoft event","description":"microsoft event for IT student"}
]}

第一个对象是一个标有“事件”的数组,它可能会让你失望。

所以,尝试替换这个:

for (NSDictionary *dict in array) {

     NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
     NSLog(@"%@",d);
     [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
}

与:

NSArray *array2 = [array objectForKey:@"event"];
for (NSDictionary *dict in array2) {

     NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
     NSLog(@"%@",d);
     [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
}

使用:Problem getting JSON contents with JSONValue

不确定这是否可行,但如果可行,对我来说会更有意义。

【讨论】:

  • 我添加了,但异常仍然存在
  • @TahanieALsanie 是的,环顾四周后,问题可能出在其他问题上……我会在答案中放一个链接。
  • 我托盘更改了我的代码,但发生这种情况:由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[NSCFString JSONValue]:无法识别的选择器发送到实例 0x6415800'
  • 我使用 kal 源来表示事件并从数据库服务器读取事件 > {"event":[{"eve_date":"2011-12-18","eve_time":"00:00: 06","title_event":"Google 活动","description":"IT 学生的 Google 活动"},{"eve_date":"2011-12-29","eve_time":"00:00:08", "title_event":"microsoft event","description":"面向 IT 学生的 Microsoft 活动"}]} 并将 JSON 与 kal 源一起用于日历
【解决方案2】:

我通过更改 JSON 的位置文件类来解决异常,但事件没有

在日历中显示。

日历显示但不包含任何事件

提示:当我将 url 放入浏览器时,数据库显示的内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多