I am working on an app that talk to Rest web service. The JSon parser I am using is SBJson. There is this web service on the server side returns a beanA. This bean contains an beanB. Part of the Json I got from the server side is like this when there is multiple beans:

        BeanB =             
        (
            {
                key = 12;
                name = test2bbb;
            },
            {
                key = 11;
                name = test2ttt;
            }
        );

when there is just one bean, it looks like this:

        BeanB =             
        {
            key = 10;
            name = test3;
        };

Passing through the SBJson parser, I got a dictionary when there is only one BeanB returned, and got an array when there is more than one BeanB returned. The code I used to receive the beans are:

        NSArray/NSDictionary *BeanB = [BeanADic objectForKey:@"beanB"];

The problem is I can't predict what I am gonna get from the server side. Is there a way to test the return type?

有帮助吗?

解决方案

This is a case where it is appropriate to check the class of the returned value.

id beanB = [beanADic objectForKey:@"tagBeans"];
if ([beanB isKindOfClass:[NSDictionary class]]) {
  NSDictionary *beanDic = beanB;
  ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top