【问题标题】:Having trouble parsing more complex JSON in iOS在 iOS 中解析更复杂的 JSON 时遇到问题
【发布时间】:2014-01-22 04:28:11
【问题描述】:

我的应用程序的一部分获取JSON 数据并使用它来显示更新。最初的JSON 响应有两个部分:错误和更新部分。

以下是你在做[JSONData objectForKey:@"updates"];隔离更新部分时得到的数组

{
2 =     {
    message = "Best restaurant ever!";
    name = " ";
    profilePictureURL = "<null>";
    restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
    restaurant = "someplace";
    timestamp = 1390194000;
};
3 =     {
    message = "Ehh.";
    name = " ";
    profilePictureURL = "<null>";
    restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
    restaurant = "someplace";
    timestamp = 1389848400;
};
4 =     {
    message = "";
    name = " ";
    profilePictureURL = "<null>";
    restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
    restaurant = "McDonald's";
    timestamp = 1390346335;
};
5 =     {
    message = "Service was slow.";
    name = " ";
    profilePictureURL = "<null>";
    restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
    restaurant = "Bad pizzaplace";
    timestamp = 1389330000;
};
}

然后我将此NSArray 传递给一个函数,该函数将其格式化为我为更新而设计的类,然后将该更新附加到存储在AppDelegate 中的NSMutableArray

@implementation Update
+(void) appendUpdates:(NSArray*)data
{


    AppDelegate* appDelegate=  [[UIApplication sharedApplication]delegate];
    for (NSDictionary* currentUpdate in data)
    {
        Update* formattedUpdate = [[Update alloc]initWithDict:currentUpdate];
        [appDelegate.updateList addObject:formattedUpdate];
    }

}
-(id) initWithDict:(NSDictionary*)update
{
    self = [super init];
    self.name = [update objectForKey:@"name"];
    self.restaurant = [update objectForKey:@"restaurant"];
    self.message = [update objectForKey:@"message"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:(int)[update objectForKey:@"timestamp"]];
    self.timestamp = date;
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
    self.dateComponents = components;
    self.profilePictureURL = [update objectForKey:@"profilePictureURL"];
    self.restaurantPictureURL = [update objectForKey:@"restPictureURL"];
    return self;
}

@end

pastebin

当我运行代码时,我在尝试使用 objectForKey: 时在初始化方法中收到错误。

有什么想法吗?提前致谢

【问题讨论】:

  • 错误是什么意思?
  • -[__NSCFString allKeys]: unrecognized selector sent to instance 0x9bc5380 2014-01-21 23:55:25.938 MyApp[49350:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString allKeys]: unrecognized selector sent to instance 0x9bc5380'
  • @JasonMarmon :看看 JSON 格式。在我的回答中。
  • 格式化代码时遇到了什么麻烦?很简单:粘贴,选择代码,点击源代码按钮。

标签: ios objective-c json parsing


【解决方案1】:

一个干净的解决方案是在您的 Update 类中创建一个字典对象,然后添加您的 keyValues 并返回该字典。还要记住你想用什么来访问字典 valueForKeyobjectForKey 中的值。他们都有区别。简读here

也许你可以格式化你的 JSON 文件

{
"updates":
 [
   {
     "ID":1,
     "SystemCovered":"Dept/State Outage Reporting",
     "InchargeFirstName":"Davids",
   },
    {
      "ID":2,
      "SystemCovered":"State Outage Reporting",
      "InchargeFirstName":"Davids-123",
   },
    {
      "ID":2,
      "SystemCovered":"Reporting",
      "InchargeFirstName":"Davids-234",
    }
 ]
}

拇指规则

在解析 JSON 时,无论何时遇到[ ],都将其视为Array,而每当您看到“{}”,则视为dictionary

希望对你有帮助。

【讨论】:

  • 我无法想象为什么我需要从 NSDictionary 继承。当方法在类Update 内部时返回self 的实例应该返回一个Update 对象,这是init 方法应该做的。我认为错误是 JSON 格式不正确,所以我会在明天与我的后端人员交谈以修复它
  • 您可能是对的,我不否认,因为我不知道您遇到了什么错误。现在我可以看到,您已经发布了异常。
  • 是的。当您这样做时,我就发现了 JSON 问题。因为我无法更改服务器端发生的事情,所以我使用 "objectForKey:@"1...2...3..."" 获得了数组成员,但这绝对是其他任何绊倒的人的最佳答案在此基础上。
  • 好的,添加一个拇指规则也:无论何时遇到[ ],都将其视为Array,而当您看到{ },则视为dictionary
【解决方案2】:

试试这个, 将您的 appendUpdates: 方法更改为

+(void) appendUpdates:(NSArray*)data
{
    AppDelegate* appDelegate=  [[UIApplication sharedApplication]delegate];
    for (NSDictionary* currentUpdate in data)
    {
        NSString *key = [[currentUpdate allKeys] objectAtIndex:0];
        currentUpdate = [currentUpdate objectForKey:key];
        Update* formattedUpdate = [[Update alloc]initWithDict:currentUpdate];
        [appDelegate.updateList addObject:formattedUpdate];
    }

}

【讨论】:

  • 没用 =/ 我认为这与 JSON 数据的开头和结尾括号有关,但我不确定如何删除它们
【解决方案3】:

将您的方法更改为+(void) appendUpdates:(NSDictionary*)data 而不是+(void) appendUpdates:(NSArray*)data。因为您有包含对象字典的字典(不是数组)。

你的方法定义应该是..

+(void) appendUpdates:(NSDictionary*)data
{
    AppDelegate* appDelegate=  [[UIApplication sharedApplication]delegate];
    for (NSString* key in [data allKeys])
    {
        NSDictionary *currentUpdate = [data objectForKey:key];
        Update* formattedUpdate = [[Update alloc]initWithDict:currentUpdate];
        [appDelegate.updateList addObject:formattedUpdate];
    }

}

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多