NSJSONSerialization and NSNull

Sometimes I come across a JSON structure as “data: null.” NSJSONSerialization turns them into NSNull, which, frankly, is pretty annoying.

I understand the reason for this - things like NSDictionary cannot contain the nil value.

This is fine, but in those cases I would prefer to see an element that is not in the de-serialized data structure, which in many cases actually facilitates the processing. Is there an option or configuration that can be passed to NSJSONSerialization and ask her to do just that - just skip these zeros in the de-serialized data structure?

+4
source share
2 answers

I do not believe that this is possible. The parameters you can pass to the class do not mention anything like this and enter into the state classAll objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

However, this can be used to your advantage. If you update a local instance of an object using the CRUD web api server, empty options will probably not be passed to JSON at all. However, if you needed the nil-ify property on the local instance, which is currently set to value, if the server sets that attr is null in JSON, you can easily check if the JSONSerialized object has this attr set to NSNull where you could not if he was absent.

Best of all, if you control the endpoint that JSON generates, you just don't need to install attr at all.

tl; dr "no."

+3

, , (read: "undefined" ) null.

(?) API- JSON , : API , , .

, , , .

, , , , ...

, null ...

0

All Articles