简体   繁体   中英

Handle object of different types

From my backend API, I get a json of objects consisting of array, dictionary, number, bool, string etc. For eg.

{
data:[
{
id : 1,
name : "abcd"
},
{
id : 2,
name : "abcde"
},
{
id : 3,
name : "abcde"
},
]
total_count : 10
}

Sometimes the value in total_count comes as a number and sometimes it comes as a string. In my code I have coded

[lbl setText:[jsonObject valueForKey:@"total_count"]]

This crashes because when the total_count key value is a number. Obviously I can do this

[lbl setText:[NSString stringWithFormat:@"%d",[[jsonObject valueForKey:@"total_count"] intValue]]];

but this happens at a lot of places in the API. A string is coming instead of a bool. data:false instead of data:[]

[EDIT]

[[AFHTTPRequestOperationManager manager] GET:[URLString attachToken] parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if([[[responseObject valueForKey:@"response"] valueForKey:@"status"] boolValue]) {
NSLog(@"success");
}
                if(success)success(operation, **responseObject**);

            } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
                if(failure)failure(operation, error);
                if(operation.response.statusCode == 0) {
                    ATAFNetworkingRequestObject *obj = [[ATAFNetworkingRequestObject alloc] init];
                    obj.urlString = URLString;
                    obj.paramters = parameters;
                    obj.successBlock = success;
                    obj.failureBlock = failure;
                    obj.type = ATNetworkingRequestGET;
                    if(![self duplicateRequestExists:obj])[pendingAPICalls addObject:obj];
                }

                [self logAPIFailedWithOperation:operation parameters:parameters error:error];
            } autoRetry:5 retryInterval:7];

do like after serilization based on your A string is coming instead of a bool. data:false instead of data:[] A string is coming instead of a bool. data:false instead of data:[]

if([datajsonObject isKindOfClass:[NSArray class]]){
    //Is array
}else if([datajsonObject isKindOfClass:[NSDictionary class]]){
    //is dictionary
}else if([datajsonObject isKindOfClass:[NSString class]])
 {
    //is String
 }
else{
    //is something else
}

You can check server value is Number or string as this

NSString *newString = [NSString stringWithFormat:@"%@",[[jsonObject valueForKey:@"total_count"]

if ([newString isKindOfClass:[NSNumber class]])
{
    NSLog(@"It is number");
}
if ([newString isKindOfClass:[NSString class]])
{
    NSLog(@"It is string");
}

Swift code :

lblCount.text = String(datajsonObject["total_count"] as AnyObject)

Objective c :

NSString *strCount = [NSString stringWithFormat:@"%@",[jsonObject valueForKey:@"total_count"]]

if ([strCount isKindOfClass:[NSString class]])
{
    // Write your code to show on label
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM