使用JSONModel(IOS)在光盘上保存对象阵列



我读了很多关于这方面的文档,但我真的不明白它是如何准确工作的。我想把我的应用程序数据以JSON格式保存在手机的光盘上。

我有一组这种类型的对象:

@interface ObjectA : NSObject
@property (strong, nonatomic) NSMutableArray* names1;
@property (strong, nonatomic) NSMutableArray* names2;
@property (strong, nonatomic) NSMutableArray* names3;
@property (strong, nonatomic) NSMutableArray* names4;
@property (strong, nonatomic) NSString* nameObjectA;
@property (assign) int number;

通过使用JSONModel,我如何在JSON文件中转换"NSMutableArray*ObjectA",然后在应用程序中读取该文件。

谢谢。

- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary {
if(self = [self init]) {
    // Assign all properties with keyed values from the dictionary
    _nameObjectA = [jsonDictionary objectForKey:@"nameAction"];
    _number = [[jsonDictionary objectForKey:@"number"]intValue];
   _actions1 = [jsonDictionary objectForKey:@"Action1"];
    _actions2 = [jsonDictionary objectForKey:@"Action2"];
    _actions3 = [jsonDictionary objectForKey:@"Action3"];
    _actions4 = [jsonDictionary objectForKey:@"Action4"];

}
return self;

}

- (NSArray *)locationsFromJSONFile:(NSURL *)url {
// Create a NSURLRequest with the given URL
NSURLRequest *request = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                     timeoutInterval:30.0];
// Get the data
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// Now create a NSDictionary from the JSON data
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// Create a new array to hold the locations
NSMutableArray *actions = [[NSMutableArray alloc] init];
// Get an array of dictionaries with the key "actions"
NSArray *array = [jsonDictionary objectForKey:@"actions"];
// Iterate through the array of dictionaries
for(NSDictionary *dict in array) {
    // Create a new Location object for each one and initialise it with information in the dictionary
    Action *action = [[Action alloc] initWithJSONDictionary:dict];
    // Add the Location object to the array
    [actions addObject:action];
}
// Return the array of actions objects
return actions;

}

JSONModel附带的演示应用程序包括一个如何通过JSONModel存储应用程序数据的示例:https://github.com/icanzilb/JSONModel

检查此视图控制器中的代码:https://github.com/icanzilb/JSONModel/blob/master/JSONModelDemo_iOS/StorageViewController.m

其逻辑是,您可以将模型导出到json字符串或符合json的字典中,然后使用标准API将其保存到光盘中。检查代码

在ObjectA中,您定义了两个方法——toDictionary和initWithDictionary。大致:

-(NSDictionary*) toDictionary {
    return @{@"names1":names1, @"names2":names2, @"names3":names3, @"names4":names4, @"nameObjectA":nameObjectA, @"number":@(number)};
}
- (id) initWithDictionary:(NSDictionary*) json {
    self = [super init];
    if (self) {
        self.names1 = json[@"names1];
        ... etc
        self.nameObjectA = json[@"nameObjectA"];
        self.number = json[@"number"].intValue;
    }
    return self;
}

通过NSJSONSerialization运行toDictionary创建的字典以生成NSData并将其写入文件。要读取,请从文件中获取NSData,通过NSJSONSerialization运行回,然后使用initWithDictionary

当然,这假设字典的内容是"JSON合法的"——字符串、数字、NSArray或其他NSDictionary。

而且,如果正在初始化的数组/字典是可变的,则应该在NSJSONSerialization上指定"MutableContainers"选项。

最新更新