当我们从ios中的json获取时,如何检查valueOfKey是Array还是Object [英] how to check valueOfKey is Array or Object when we get it from json in ios
本文介绍了当我们从ios中的json获取时,如何检查valueOfKey是Array还是Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个案例要解析 JSON
首先是:
I have 2 cases to parse JSON
First is:
{
"post_filter_data": {
"Items": [
{
"ItemID": "50cb4e46b5d30b0002000009",
"ItemName": "Fruit salad test",
"ItemPrice": "122.0",
"ItemDescription": "test test",
"ItemImageUrl": "http://s3.amazonaws.com/menubis-mms-prototype-dev/menubis/assets/50cb4e64b5d30b0002000013/landing_page.jpg?1355501156"
},
{
"ItemID": "50d0870d910ef2000200000a",
"ItemName": "test new",
"ItemPrice": "120.0",
"ItemDescription": null,
"ItemImageUrl": "http://s3.amazonaws.com/menubis-mms-prototype-dev/menubis/assets/50d0871a910ef20002000015/Screenshot-2.png?1355843354"
}
]
}
}
in哪些Items是 NSArray
并且它很容易解析,但是当只有一个对象时我会通过异常。
秒 JSON
其中Items标签有一个对象:
in which Items is an NSArray
and it's parse easily but when only one object I get its through exception.
Second JSON
is in which Items tag has one one object:
{
"post_filter_data": {
"Items": {
"ItemID": "50d1e9cd9cfbd20002000016",
"ItemName": "test",
"ItemPrice": "120.0",
"ItemDescription": "test",
"ItemImageUrl": "http://s3.amazonaws.com/menubis-mms-prototype-dev/menubis/assets/50d1ea019cfbd20002000022/11949941671787360471rightarrow.svg.med.png?1355934209"
}
}
}
我的代码就在这里我在解析它:
and my code is here In which I am parsing it:
NSDictionary *dictMenu=[responseDict valueForKey:@"post_filter_data"];
NSArray* subMenuArray=[dictMenu valueForKey:@"Items"];
有什么方法可以检查出 valueForKey:@项目
是数组
或对象
。
Is there any way in which I check it out that valueForKey:@"Items"
is Array
or Object
.
推荐答案
在_recievedData中获取数据rx然后检查对象的类。
Get data rx in _recievedData then check the class of the object.
id object = [NSJSONSerialization
JSONObjectWithData:_recievedData
options:kNilOptions
error:&error];
if (error)
{
NSLog(@"Error in rx data:%@",[error description]);
}
if([object isKindOfClass:[NSString class]] == YES)
{
NSLog(@"String rx from server");
}
else if ([object isKindOfClass:[NSDictionary class]] == YES)
{
NSLog(@"Dictionary rx from server");
}
else if ([object isKindOfClass:[NSArray class]] == YES)
{
NSLog(@"Array rx from server");
}
这篇关于当我们从ios中的json获取时,如何检查valueOfKey是Array还是Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文