How to include null value in the JSON via NSJSONSerialization?

前端 未结 4 680
情深已故
情深已故 2020-12-30 22:25

I think I get it how to use the NSJSONSerialization over all. The call I am making is:

[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&a         


        
4条回答
  •  情书的邮戳
    2020-12-30 23:22

    You have to use NSNull. For instance

    Swift

    let dict = ["firstKeyHasValue": 2342, "secondKeyHasNoValue": NSNull()]
    

    Objective-C

    NSDictionary *dict = @{ @"error": [NSNull null] };
    

    From the official documentation of NSDictionary:

    Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.

提交回复
热议问题