0

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];
Swati
  • 1,417
  • 1
  • 15
  • 35

3 Answers3

1

do like after serilization based on your 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
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • thanks for your answer Anbu but I cannot do this for all the objects or is it a good practice to do this? – Swati Mar 24 '17 at 06:30
  • better create the common code in appdelegate or NSobject class, pass your JSON object to this method, then get the return type of object type and handle it – Anbu.Karthik Mar 24 '17 at 06:31
  • see this for example http://stackoverflow.com/questions/1144629/in-objective-c-how-do-i-test-the-object-type – Anbu.Karthik Mar 24 '17 at 06:37
0

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");
}
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
0

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
}
Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28