2

我在尝试将 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();
}
}

因此,在这种情况下,我在列表视图中看到了这两个对象,但它们为空,没有任何值。我该如何解决?

4

1 回答 1

0
  • 您正在为 > IOS5.0 开发吗?

  • 我使用NSJSONReadingMutableLeaves而不是kNilOptions

    [NSJSONSerialization JSONObjectWithData:returnData 选项:NSJSONReadingMutableLeaves 错误:&error];

于 2012-09-09T14:14:15.570 回答