JSON解析到Core Data有效,但我得到了null对象

时间:2012-09-09 13:54:27

标签: ios parsing core-data json

我在尝试将JSON保存到Core Data时遇到的问题是我在列表视图中获取了空对象,而不是使用数据填充它们。这是我的JSON格式:

[{"nid":"23","name":"JN01"},{"nid":"24","name":"JN02"}]

这是我的代码

jsonToArray.h

#define JSON_NAME @"name"
#define JSON_NID @"nid"
@interface JSONToArray : NSObject
@property (nonatomic, copy) NSString *nid;
@property (nonatomic, copy) NSString *name;
+ (void)showNetworkError:(NSString *)theMessage;
+ (NSArray*)retrieveJSONItems;

jsonToArray.m

+ (NSArray*)retrieveJSONItems { //skipped a few boring lines
NSArray *json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url]
                                                options:kNilOptions
                                                  error:&err];
return json;}

Job+Json.h //Job- my entity

+ (Job *)appsWithJSONInfo:(NSDictionary *) nbJSONInfo
inManagedObjectContext:(NSManagedObjectContext *)context;
+ (Job *)appsWithID:(NSString *)newID inManagedObjectContext:(NSManagedObjectContext *)context;

Job+Json.m

#import "Apps+JSON.h"
#import "JSONToArray.h"

@implementation Job (JSON)

+ (Job *)appsWithJSONInfo:(NSDictionary *) nbJSONInfo
inManagedObjectContext:(NSManagedObjectContext *)context {

Job *apps = nil;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Job"];
request.predicate = [NSPredicate predicateWithFormat:@"nid=%@", [nbJSONInfo objectForKey:JSON_NID]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];

if (!matches || ([matches count] > 1)) {
    [JSONToArray showNetworkError:@"There was an error retrieving the json Data."];
} else if ([matches count] == 0) {
    apps = [NSEntityDescription insertNewObjectForEntityForName:@"Job" inManagedObjectContext:context];
    apps.nid = NSLocalizedString([nbJSONInfo objectForKey:JSON_NID], nil);
    apps.name = NSLocalizedString([nbJSONInfo objectForKey:JSON_NAME], nil);



}else {
    apps = [matches lastObject];
}

return apps;
}

+ (Job *)appsWithID:(NSString *)newID
            inManagedObjectContext:(NSManagedObjectContext *)context
{
Job *app = nil;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Job"];
request.predicate = [NSPredicate predicateWithFormat:@"nid = %@", newID];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

NSError *error = nil;
NSArray *apps = [context executeFetchRequest:request error:&error];

if ([apps count] == 0) {
    app = [NSEntityDescription insertNewObjectForEntityForName:@"Job"
                                        inManagedObjectContext:context];
    app.nid = NSLocalizedString(newID, nil);
} else if ([apps count] >=1) {   
    app = [apps lastObject];
}
return app;
}

最后我的列表视图:

- (void)viewWillAppear:(BOOL)animated{
[self fetchJSONDataIntoDocument:self.managedObjectContext];}

- (void)fetchJSONDataIntoDocument:(NSManagedObjectContext *)managedObjectContext{    
NSArray * tempArray = [JSONToArray retrieveJSONItems];
for (NSDictionary *JSONInfo in tempArray) {
[Job appsWithJSONInfo:JSONInfo inManagedObjectContext:managedObjectContext];

}

NSError *error = nil;
if (![managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
}

所以在这种情况下,我在列表视图中看到了两个对象,但它们都是null,没有值。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

  • 您是否正在开发> IOS5.0?

  • 我使用 NSJSONReadingMutableLeaves 代替 kNilOptions

    [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:& error];