Write JSON Response to .plist File

前端 未结 6 1621
遥遥无期
遥遥无期 2021-02-20 11:14

Frustration on the Top !!!

I am getting some JSON Response from the Service and I want to store it in the .plist file

6条回答
  •  故里飘歌
    2021-02-20 12:03

    I bet that your JSON contains at least one null value.

    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. And for dictionaries, the keys must be NSStrings.

    You can also use mutable subclasses (like NSMutableString) when writing.

    If you have anything not on that list, you can't use writeToURL:atomically or any of the similar convenience methods.

    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).

    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.

提交回复
热议问题