将JSON响应写入.plist文件 [英] Write JSON Response to .plist File
问题描述
顶部的挫折!!!
我从服务部门获得了一些 JSON响应希望将其存储在 .plist文件中以供将来参考。
我无法将 JSON响应保存到.plist文件。我认为这是由于响应中的一些 null
值。
I am getting some JSON Response from the Service and I want to store it in the .plist file for Future Reference.
I am unable to save my JSON Response to .plist File. I think it's due to some null
values into the Response.
注意: 我使用 jsonparser 确认响应为 JSON格式。
我的代码:
NSError *error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *dictResult = [(NSDictionary*)json objectForKey:@"myKey"];
NSLog(@"Result Dictionary :: %@",dictResult);
NSURL *cacheDir = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *path = [cacheDir URLByAppendingPathComponent:@"FinalResult.plist"];
NSLog(@"Path :: %@",path);
BOOL success = [dictResult writeToURL:path atomically:YES];
NSLog(@"success? %d", success);
注意: 我获得了所有 NSLog值(表示响应词典和文件路径,但 0表示成功)。
Note : I got all the NSLog Values (means the Response Dictionary and File Path but 0 for success).
问题:在响应中我几乎有 70-80个键值对不希望删除/替换所有 null
值。因为我想要的是......
Problem : There are almost 70-80 key-value pairs in the Response and I don't want to remove/replace all the null
values. Because What I want is ...
- 从服务器获取响应。
- 使用响应填充所有 UITextFields 。
- 使用一些已编辑的值向服务器发布相同的响应 UITextFields 。
- GET the Response From the Server.
- Fill all the UITextFields with the Response.
- POST the Same Response to the Server with some Edited Values from the UITextFields.
所以,我只想更改已编辑对象中的 UITextField
值,让它POST到服务器。
So, I just want to change the Edited UITextField
values in the Object and let it POST to the Server.
什么是最佳方式解决这个问题?
推荐答案
我敢打赌你的JSON包含至少一个 null
value。
I bet that your JSON contains at least one null
value.
如果您的JSON包含 null
并使用<$ c进行转换$ c> NSJSONSerialization , null
由 NSNull
的实例替换。如果你的字典包含 NSNull
,那么 writeToURL:atomically:
将失败。
When you have JSON that contains null
and you convert it using NSJSONSerialization
, the null
is replaced by an instance of NSNull
. If your dictionary contains NSNull
, then writeToURL:atomically:
will fail.
这是因为只有当集合中的数据仅限于属性列表类型时,读取和写入字典(和数组)的便捷方法才有效。这些是:
This is because the convenience methods for reading and writing dictionaries (and arrays) only work if the data in the collection is restricted to property list types. These are:
-
NSString
-
NSNumber
-
NSData
-
NSDate
-
NSArray
-
的NSDictionary
。对于词典,键必须是NSString
s。
NSString
NSNumber
NSData
NSDate
NSArray
NSDictionary
. And for dictionaries, the keys must beNSString
s.
你也可以在写作时使用可变子类(如 NSMutableString
)。
You can also use mutable subclasses (like NSMutableString
) when writing.
如果你的列表中没有任何内容,你可以'使用 writeToURL:atomically
或任何类似的便捷方法。
If you have anything not on that list, you can't use writeToURL:atomically
or any of the similar convenience methods.
问题是一些有效的JSON可以'转换为属性列表。此外,一些有效的属性列表无法转换为JSON(因为 NSDate
不会自动转换为有效的JSON)。
The problem is that some valid JSON can't be converted to property lists. Also, some valid property lists can't be converted to JSON (because NSDate
won't automatically convert to valid JSON).
如果是我,我只是将数据写入JSON文件。保留原始格式。您可以轻松地转换为JSON,也可以从JSON转换。所以请保持这种状态。
If it was me, I'd just write the data to a JSON file. Leave it in its original format. You can convert to/from JSON easily, so leave it that way.
这篇关于将JSON响应写入.plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!