通过" key":" value"将jsondata保存到plist格式

时间:2015-11-02 20:52:24

标签: ios plist

这里我获取数据并将其转换为nsdictionary并使用plist进行存储。我还放了一些NSLog来检查我的数据是否已经存储。但是当我读到我的plist时,我已经存储了json。在我的控制台中它说Null。

code is edited

所以这是我的代码。我想要的任何东西或任何其他方式如" Key" - " Value"格式将我的json数据存储到plist。

您的帮助将转移到下一个功能。谢谢。

如果" Key" /" value"有任何其他方法可用,请通过代码告诉我。因为我是ios&的新手。代码不好

这是我的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)

2 个答案:

答案 0 :(得分:1)

我测试了这个并且它有效。还可以更好地使用您未使用的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从网络资源中提取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.
}

答案 1 :(得分:0)

自从你写了

  

将其转换为nsdictionary并使用plist存储

并且JSON确认(它以花括号开头),它不能是数组。

相反

NSArray *stringsArray = [NSArray arrayWithContentsOfFile:plistPath];

NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

PS:我建议完全使用与URL相关的API