0

我阅读了很多关于此的文档,但我无法真正理解它是如何工作的。我想将我的应用程序数据以 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;

}

4

2 回答 2

2

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

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

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

于 2014-09-25T12:42:36.180 回答
-1

在 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;
}

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

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

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

于 2014-09-25T12:32:56.383 回答