在iOS中解析更复杂的JSON时遇到问题

时间:2014-01-22 04:28:12

标签: ios objective-c json parsing

我的应用程序的一部分获取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传递给一个函数,该函数将其格式化为我为更新设计的类,然后将该更新附加到NSMutableArray中存储的AppDelegate

@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:时,我在初始化方法中收到错误。

有什么想法吗?提前致谢

3 个答案:

答案 0 :(得分:0)

试试这个, 将您的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];
    }

}

答案 1 :(得分:0)

一个干净的解决方案就是在Update课程中创建一个字典对象。然后添加你的keyValues&amp;返回那本字典。另请注意,您要使用哪些内容来访问字典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",
    }
 ]
}

THUMB RULE

在解析JSON时,每当遇到[ ]时,请将其视为Array&amp;无论何时看到“{}”,处理都在dictionary

我希望有所帮助。

答案 2 :(得分:0)

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

你的方法定义应该是..

+(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];
    }

}