我花了很多时间寻找这个看似简单的问题的答案。
所以这些是我的解析方法:
-(void)ParseJSON {
NSURL *URL=[[NSURL alloc]initWithString:@"http://www.foo24.com/kokos"];
NSData* data = [NSData dataWithContentsOfURL:
URL];
[self performSelectorOnMainThread:@selector(JSONtoArrays:)
withObject:data waitUntilDone:YES];
}
-(void)JSONtoArrays:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSMutableArray *fetchCustomersArray=[json objectForKey:@"Customers"];
NSMutableArray *fetchOrdersArray=[json objectForKey:@"Orders"];
[self setJSONCustomers:fetchCustomersArray];
[self setJSONOrders:fetchOrdersArray];
}
这是我从数据库中获取的方法:
-(void)fetchOrders{
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSEntityDescription *orderEntity=[NSEntityDescription entityForName:@"OrderEntity" inManagedObjectContext:managedObjectContext];
NSPredicate *NonDelivered =
[NSPredicate predicateWithFormat:@"orderDelivered == NO"];
//setup fetch request
NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setEntity:orderEntity];
[request setPredicate:NonDelivered];
//fetch
NSError *error;
NSMutableArray *FetchResultsArray=[[managedObjectContext executeFetchRequest:request error:&error]mutableCopy];
[self setDbOrders:FetchResultsArray];
[dbOrders addObjectsFromArray:JSONOrders];
NSLog(@"%@",[[dbOrders objectAtIndex:0]valueForKey:@"orderName"]);
[self addOrders:dbOrders];
}
我想要做的是从核心数据中检索未交付的项目作为数组,然后获取新的解析数组,从数据库中删除所有条目,合并两个数组并将它们放回数据库中。我不知道是否有更简单的方法可以做到这一点。