我阅读了很多关于此的文档,但我无法理解它是如何工作的。 我想将我的应用数据以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,我如何转换" NSMutableArray * ObjectA"在JSON文件中,之后在应用程序中读取此文件。
感谢。
- (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;
}
答案 0 :(得分:2)
JSONModel附带的演示应用程序包含一个如何通过JSONMOdel存储应用程序数据的示例:https://github.com/icanzilb/JSONModel
检查此视图控制器中的代码:https://github.com/icanzilb/JSONModel/blob/master/JSONModelDemo_iOS/StorageViewController.m
逻辑是您可以将模型导出到json字符串或json兼容字典,然后使用标准API将它们保存到光盘。检查代码
答案 1 :(得分:-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合法" - 字符串,数字,NSArrays或其他NSDictionarys。
并且,如果正在初始化的数组/字典是可变的,那么应该指定" MutableContainers" NSJSONSerialization上的选项。