Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过“将jsondata保存到plist”;“关键点”是:&引用;“价值”;格式_Ios_Plist - Fatal编程技术网

Ios 通过“将jsondata保存到plist”;“关键点”是:&引用;“价值”;格式

Ios 通过“将jsondata保存到plist”;“关键点”是:&引用;“价值”;格式,ios,plist,Ios,Plist,在这里,我获取数据并将其转换为nsdictionary,然后使用plist进行存储。我还放了一些NSLog来检查我的数据是否已存储。但当我读取plist时,我已经存储了json。在我的控制台中,它说Null code is edited 这是我的代码。我会做我遗漏的任何事情,或者用“Key”-“Value”格式将json数据存储到plist 您的帮助将转到下一个功能。请提前感谢 如果有任何其他方法可以使用“Key”/“value”-请在代码中告诉我。因为我是ios新手&代码不太好 这是我的Js

在这里,我获取数据并将其转换为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)
自从你写了

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

JSON还确认(它以一个大括号开始),它不能是数组

反而

NSArray *stringsArray = [NSArray arrayWithContentsOfFile:plistPath];


PS:我建议完全使用与URL相关的API,我已经对此进行了测试,并且效果良好。还可以更好地使用未使用的N错误。它也不依赖于已经创建的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.
}

请显示日志输出。还有,你为什么要写字典和读数组?还可以使用
[jsonResults writeToFile:plistPath atomicaly:YES]直接写入文件
而不是首先将其转换为
NSData
(我担心您会重新分配
returnData
)。@特洛伊木马程序我已经更新了我的代码,我对代码一无所知。如果可能,请用您的答案重写我的代码,以便我能学到一些东西。如果可能,请用代码更新您的答案。我无法理解你对重新分配
returnData
还有一个问题的看法……我的json有4个类别。每个类别中都有一些id、文件、图像……所以当我通过iphone看到我的plist时,它看起来如何(即我的json数据)json只是文本。
NSJSONSerialization
类将字典/数组结构转换为对应的Cocoa结构,将字符串/数字值转换为string/Int/Float。
NSPropertyListSerialization
类从字典/数组中创建一个具有适当XML头签名的属性列表。plist是否仍然首选存储类似json的数据??感谢您的解决方案……如果可能,请为另一篇文章提供一些解决方案。在我的朋友与代码斗争的地方——这是一种非常方便的方式,因为JSON类型与属性列表兼容。您的代码是否会将我的json响应数据保存为“key”/“value”格式,并在您的第一行代码
NSString*returnData=@“{}”
中保存一个内容,以便传递json数据..但我只给出了一个示例。我的json中有50个文件。我将如何将所有文件放入
NSString*returnData=@“{}”
——您是从web源代码还是从磁盘上的文件或其他东西中提取json?是的,NSDictionary是键:值对。如果可能的话,请尝试为我的朋友提供解决方案,他是ios新手,正如@vadian所说的,你应该使用一个网络库来发出HTTP请求并获取JSON。看看AFN,它非常容易使用,并且有很多例子。它还将为您执行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.
}