NSJSONSerialization returns “<null>” string

旧巷老猫 提交于 2019-12-05 08:51:32

There is nothing I guess, though it can be easily corrected from api makers, if not possible, you can always put a simple macro, I use to avoid such thing, follow macro below

#define Is_Empty(value) (value == (id)[NSNull null] || value == nil || ([value isKindOfClass:[NSString class]] && ([value isEqualToString:@""] ||  [value isEqualToString:@"<null>"]))) ? YES : NO

#define IfNULL(original, replacement) IsNULL(original) ? replacement : original

#define IsNULL(original) original == (id)[NSNull null]

#define SafeString(value) IfNULL(value, @"")

Usage

self.label.text=SafeString([dic objectForKey:@"name"]);
gnasher729

You are wrong. null values in JSON are not stored as <null> string values. They are stored as NSNull objects, which NSLog logs as <null>.

You can never trust what data you were given. If you assume you got an NSString and the server sends you a number, your code is likely to crash.

NSString* myJSONString = ...;
if ([myJSONString isKindOfClass:[NSString class]]) {
    it is a string
} else {
    it is not a string
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!