按"key":"value"格式保存jsondata到plist
save jsondata to plist by "key":"value" format
我在这里获取数据并将其转换为 nsdictionary 并使用 plist 进行存储。我还放了一些 NSLog 来检查我的数据是否已存储或 not.but 当我阅读我的 plist 时我存储了 json 。在我的控制台中它说 Null 。
code is edited
所以这是我的 code.Does 我遗漏的任何东西或任何其他方式,如 "Key"-"Value" 格式,用于将我的 json 数据存储到 plist。
您的帮助将转到下一个功能。在此先致谢。
如果 "Key"/"value" 有任何其他方法可用-请在 code.Because 中告诉我14=]
这是我的Json。另请在此处查看它有 4 个类别。在每个类别中它都有一些 id、数据、日期..
日志输出:
plistPath : /Users/maicr/Library/Developer/CoreSimulator/Devices/144C1221-F589-415C-8855-3E5AA06459F8/data/Containers/Data/Application/6F405C7F-43F4-4910-B2CF-15083345F38C/Documents/SampleData.plist
2015-11-03 01:54:12.156 demo[10450:550543] Data successfully saved.
2015-11-03 01:54:12.157 demo[10450:550543] str: (null)
因为你写了
convert it in to nsdictionary and use plist to store
并且 JSON 确认(它以大括号开头),它不能是数组。
相反
NSArray *stringsArray = [NSArray arrayWithContentsOfFile:plistPath];
写
NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
PS:我建议完全使用URL相关的API
我已经对此进行了测试并且有效。还可以更好地使用您未使用的 NSErrors。它也不依赖于已经创建的 plist..
NSString *returnData = @"{\"count\":3,\"most\":{\"count\":0,\"files\":[]},\"deleted\":{\"count\":0,\"files\":[]},\"original\":{\"count\":4,\"files\":[{\"created_time\":1431939838,\"last_modified_time\":1431939859,\"id\":8293015,\"name\":\"doc1.pdf\"},{\"created_time\":1431939845,\"last_modified_time\":1431939869,\"id\":8293017,\"name\":\"doc2.pdf\"},{\"created_time\":1431939854,\"last_modified_time\":1431939886,\"id\":8293019,\"name\":\"doc3.pdf\"},{\"created_time\":1431939872,\"last_modified_time\":1431939898,\"id\":8293023,\"name\":\"doc4.pdf\"}]},\"extra\":{\"count\":0,\"files\":[]}}";
NSData *data = [returnData dataUsingEncoding:NSUTF8StringEncoding]; // Your data may start as NSData to begin with...
NSError *parseError = nil;
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parseError];
if(parseError != nil )
{
NSLog(@"Parse Error: %@", parseError.userInfo);
}
else
{
//Build plist path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"SampleData.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *plistData = nil;
if([fileManager fileExistsAtPath:path])
{
// Pull existing plist
plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
}
else
{
// Create new plist
plistData = [NSMutableDictionary new];
}
[plistData setObject:jsonResults forKey:@"myJSONData"];
[plistData writeToFile:path atomically:TRUE];
// .....
// Retrieve Data..
NSMutableDictionary *savedJSONResults = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
if([[savedJSONResults allKeys] containsObject:@"myJSONData"])
{
NSDictionary *myJSONData = [savedJSONResults objectForKey:@"myJSONData"];
NSLog(@"%@", myJSONData);
}
}
使用 AFNetworking API
从 Web 源中提取 JSON
[AFHTTPRequestOperationManager manager] GET:@"www.someAPI.com/resource" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSDictionary *JSONKeyValuePairs = (NSDictionary *)responseObject; // serialized JSON data for you to do stuff with...
// Do PLIST saving stuff here...
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
NSAssert(false, @"I failed."); // do something better than asset for release build since they are not compiled in. EI handle failures gracefully somehow.
}
我在这里获取数据并将其转换为 nsdictionary 并使用 plist 进行存储。我还放了一些 NSLog 来检查我的数据是否已存储或 not.but 当我阅读我的 plist 时我存储了 json 。在我的控制台中它说 Null 。
code is edited
所以这是我的 code.Does 我遗漏的任何东西或任何其他方式,如 "Key"-"Value" 格式,用于将我的 json 数据存储到 plist。
您的帮助将转到下一个功能。在此先致谢。
如果 "Key"/"value" 有任何其他方法可用-请在 code.Because 中告诉我14=]
这是我的Json。另请在此处查看它有 4 个类别。在每个类别中它都有一些 id、数据、日期..
日志输出:
plistPath : /Users/maicr/Library/Developer/CoreSimulator/Devices/144C1221-F589-415C-8855-3E5AA06459F8/data/Containers/Data/Application/6F405C7F-43F4-4910-B2CF-15083345F38C/Documents/SampleData.plist
2015-11-03 01:54:12.156 demo[10450:550543] Data successfully saved.
2015-11-03 01:54:12.157 demo[10450:550543] str: (null)
因为你写了
convert it in to nsdictionary and use plist to store
并且 JSON 确认(它以大括号开头),它不能是数组。
相反
NSArray *stringsArray = [NSArray arrayWithContentsOfFile:plistPath];
写
NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
PS:我建议完全使用URL相关的API
我已经对此进行了测试并且有效。还可以更好地使用您未使用的 NSErrors。它也不依赖于已经创建的 plist..
NSString *returnData = @"{\"count\":3,\"most\":{\"count\":0,\"files\":[]},\"deleted\":{\"count\":0,\"files\":[]},\"original\":{\"count\":4,\"files\":[{\"created_time\":1431939838,\"last_modified_time\":1431939859,\"id\":8293015,\"name\":\"doc1.pdf\"},{\"created_time\":1431939845,\"last_modified_time\":1431939869,\"id\":8293017,\"name\":\"doc2.pdf\"},{\"created_time\":1431939854,\"last_modified_time\":1431939886,\"id\":8293019,\"name\":\"doc3.pdf\"},{\"created_time\":1431939872,\"last_modified_time\":1431939898,\"id\":8293023,\"name\":\"doc4.pdf\"}]},\"extra\":{\"count\":0,\"files\":[]}}";
NSData *data = [returnData dataUsingEncoding:NSUTF8StringEncoding]; // Your data may start as NSData to begin with...
NSError *parseError = nil;
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parseError];
if(parseError != nil )
{
NSLog(@"Parse Error: %@", parseError.userInfo);
}
else
{
//Build plist path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"SampleData.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *plistData = nil;
if([fileManager fileExistsAtPath:path])
{
// Pull existing plist
plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
}
else
{
// Create new plist
plistData = [NSMutableDictionary new];
}
[plistData setObject:jsonResults forKey:@"myJSONData"];
[plistData writeToFile:path atomically:TRUE];
// .....
// Retrieve Data..
NSMutableDictionary *savedJSONResults = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
if([[savedJSONResults allKeys] containsObject:@"myJSONData"])
{
NSDictionary *myJSONData = [savedJSONResults objectForKey:@"myJSONData"];
NSLog(@"%@", myJSONData);
}
}
使用 AFNetworking API
从 Web 源中提取 JSON[AFHTTPRequestOperationManager manager] GET:@"www.someAPI.com/resource" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSDictionary *JSONKeyValuePairs = (NSDictionary *)responseObject; // serialized JSON data for you to do stuff with...
// Do PLIST saving stuff here...
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
NSAssert(false, @"I failed."); // do something better than asset for release build since they are not compiled in. EI handle failures gracefully somehow.
}