how to check valueOfKey is Array or Object when we get it from json in ios

前端 未结 2 512
一生所求
一生所求 2021-01-06 07:40

I have 2 cases to parse JSON First is:

{
    \"post_filter_data\": {
        \"Items\": [
            {
                \"ItemID\": \"50cb4e46b5         


        
相关标签:
2条回答
  • 2021-01-06 08:25

    Yes, you can check using class like

    if ([[dictMenu valueForKey:@"Items"] isKindOfClass:[NSArray class]])
    {
        // array inside
    }
    
    0 讨论(0)
  • 2021-01-06 08:26

    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");
        }
    
    0 讨论(0)
提交回复
热议问题