0

Server side execute a SQL query (server is written in python) returns a json which looks like this:

return HttpResponse(json.dumps([{"data":output, "total":theResult}]), content_type ='application/json')

output is a result return from inner method and is already serialized like this:

output = serializers.serialize('json',p_list,fields=('price','publishdate','size'))

Client side receive successfully the response, success:^(AFHTTPRequestOperation *operation, id responseObject) using AFHTTPRequestOperationManager and this is what responseObject looks like in debug console (lldb)

po responseObject
<__NSCFArray 0x116fa6190>(
{
    data = "[{\"pk\": 817, \"model\": \"xx\", \"fields\": { \"price\": \"3300\", \"publishdate\": \"2014-10-30T00:00:00\", \"size\": 35}}, {\"pk\": 2799, \"model\": \"xx\", \"fields\": { \"price\": \"6250\", \"publishdate\": \"2014-12-08T00:00:00\",\"size\": 0}}]";
    total =     (
       381
    );
}
)

in console po responseObject[0][@"data"][0] prints the data array and p responseObject[0][@"total"][0] print 381 as expected

The Problem:

  1. in code trying to cast responseObject[0][@"total"][0] into integer return a garbage number
  2. casting responseObject[0][@"data"][0] into NSArray* and then trying to perform count or any other operation causes an exception: 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance
liv a
  • 3,232
  • 6
  • 35
  • 76
  • What does "cast responseObject[0][@"total"][0] into integer" mean and where is the code. – zaph Jan 22 '15 at 16:38
  • @Zaph casting means doing `int total = (int )(responseObject[0][@"total"][0])` or `int total = [responseObject[0][@"total"][0] intValue];` – liv a Jan 22 '15 at 16:57

1 Answers1

0

Part of the problem may be that the description method does not quote all strings so 381 may be a string.

Posible solutions (lacking OP code):

if it is really an int use:

int value = responseObject[0][@"total"][0] 

if it is really a string:

NSString *valueString = responseObject[0][@"total"][0] 
int value = [valueString intValue];
zaph
  • 111,848
  • 21
  • 189
  • 228