NSJSONSerialization返回"< null>"串 [英] NSJSONSerialization returns "<null>" string

查看:167
本文介绍了NSJSONSerialization返回"< null>"串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将NSDictionary设置为从服务器检索到的JSON对象,我在这一行中这样做:

I'm try to set an NSDictionary to a JSON object retrieved from the server, I'm doing that in this line:

_peopleArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

它工作正常并正确创建字典。但是,我有一个问题,JSON对象中 null 的值存储为< null>字典中的字符串值。有没有办法解决这个问题或解决它?我想避免遍历整个事情并将它们设置为 @

It works fine and properly creates the dictionary. However, I have a problem, values that are null in the JSON object are stored as "<null>" string values in the dictionary. Is there any way to fix this or work around it? I want to avoid traversing through the entire thing and setting them to @"".

感谢您的帮助!

~Carpetfizz

~Carpetfizz

推荐答案

我猜没有什么,虽然它可以很容易地从api制造商纠正,如果不可能,你可以随时放一个简单的宏,我用来避免这样的事情,按照下面的宏

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, @"")

用法

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

这篇关于NSJSONSerialization返回&quot;&lt; null&gt;&quot;串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆