我可以最小化此 SBjson 代码中使用的对象数量吗?
我从服务器收到的响应的格式如下:
{
"Data":{
"Key": "Value"
...
},
"Key": "Value"
...
}
但是,我只对“数据”下的元素感兴趣。 这是我当前使用的代码:
SBJsonParser *parser = [SBJsonParser new];
NSString *responseString = [request responseString];
NSDictionary *responseData = [parser objectWithString:responseString];
NSString *infoString = [responseData objectForKey:@"Data"];
NSDictionary *infoData = [parser objectWithString:infoString];
有没有一种方法可以在不显式声明 5 个对象的情况下执行相同的操作?只是寻找一些我应该使用的速记方式。
The response I receive from the server is formatted as such:
{
"Data":{
"Key": "Value"
...
},
"Key": "Value"
...
}
However, I am only interested in the elements under "Data".
Here is the code I'm currently using:
SBJsonParser *parser = [SBJsonParser new];
NSString *responseString = [request responseString];
NSDictionary *responseData = [parser objectWithString:responseString];
NSString *infoString = [responseData objectForKey:@"Data"];
NSDictionary *infoData = [parser objectWithString:infoString];
Is there a way to perform the same thing without explicitly declaring 5 objects? Just looking for some sense of short-hand that I should be using.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的最后两行是错误的 -
"Data"
实际上是一个NSDictionary
,所以你不需要双重解析它。此外,大多数 Objective-C 程序员都会在他们知道返回是安全的地方嵌套调用 - 我的意思是不需要额外的检查。例如,这对我来说是一个更自然的实现:
请注意,我正在使用
SBJSON
附带的 NSObject 类别中的便捷方法JSONValue
。Your last two lines are wrong -
"Data"
is actually anNSDictionary
, so you don't need to double parse it.Also, most objective-C programmers would nest calls where they know that the returns are safe - by which I mean don't need additional checking. For instance, this would see a more natural implementation to me:
Note that I am using the convenience method
JSONValue
from the category on NSObject that comes withSBJSON
.