使用JSONModel(IOS)将对象数组保存在光盘上 [英] Save array of objects on the disc with JSONModel (IOS)
问题描述
我阅读了很多关于此的文档,但是我真的不明白它是如何工作的. 我想将应用程序数据以JSON格式保存在手机的光盘上.
I read a lot of docs about this but I can't really understand how it precisely works. I would like to save my apps data in JSON format on the disc of the phone.
我有一系列这种类型的对象:
I have a array of objects of this type:
@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",然后再在应用程序中读取该文件.
By using JSONModel, how can I transforme a "NSMutableArray *ObjectA" in a JSON file and after that read this file back in the app.
谢谢.
- (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存储应用程序的数据:
The demo app that comes with JSONModel includes an example how to store your app's data via a JSONMOdel: https://github.com/icanzilb/JSONModel
在此视图控制器中检查代码: https://github.com. com/icanzilb/JSONModel/blob/master/JSONModelDemo_iOS/StorageViewController.m
Check the code in this view controller: https://github.com/icanzilb/JSONModel/blob/master/JSONModelDemo_iOS/StorageViewController.m
逻辑是您可以将模型导出到json字符串或json兼容字典,然后使用标准API将其保存到光盘.检查代码
The logic is that you can export your model to a json string or json compliant dictionary and then save those to the disc using the standard APIs. Check the code
这篇关于使用JSONModel(IOS)将对象数组保存在光盘上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!