如何检查valueOfKey是Array或Object,当我们从ios中获取json时(how to check valueOfKey is Array or Object when we get i

编程入门 行业动态 更新时间:2024-10-14 08:22:52
如何检查valueOfKey是Array或Object,当我们从ios中获取json时(how to check valueOfKey is Array or Object when we get it from json in ios)

我有2个案例来解析JSON首先是:

{ "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" } ] } }

其中Items是一个NSArray ,它很容易解析,但是当只有一个对象通过异常得到它时。 第二个JSON中的Items标签有一个对象:

{ "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" } } }

和我的代码在这里我解析它:

NSDictionary *dictMenu=[responseDict valueForKey:@"post_filter_data"]; NSArray* subMenuArray=[dictMenu valueForKey:@"Items"];

有什么方法可以检查出valueForKey:@"Items"是Array还是Object 。

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 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"];

Is there any way in which I check it out that valueForKey:@"Items" is Array or Object.

最满意答案

在_recievedData中获取数据rx,然后检查对象的类。

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"); }

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"); }

更多推荐

本文发布于:2023-08-07 01:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我们   Object   Array   valueOfKey   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!