0
votes

I'm having a problem on parsing a JSON. That is my code.

-(void)requstJson1:(ASIHTTPRequest *)request
{
    jsonResponse = [request responseString];
    jsonDic = [jsonResponse JSONValue];
    jsonResult = [[jsonDic valueForKey:@"events"] retain];
    type=[[jsonResult valueForKey:@"time"] retain];
    data=[[jsonResult valueForKey:@"data"] retain];
    NSDictionary *dic=[data JSONValue];
    hp=[[dic valueForKey:@"hp"] retain];
}

That is my JSON Response

{
    "result": "ok",
    "totalcount": 422,
    "events": [
        {
            "id": "52982168e4b00e53abdace66",
            "deviceId": "203",
            "channelId": "",
            "data": "{\"hp\":\"6586129568\",\"camID\":\"camID120\",\"device_id\":38,\"pairedDeviceId\":\"204\"}",
            "longitude": 103.82,
            "latitude": 1.352,
            "type": "event-intercom-visitor-alert",
            "time": "29/11/2013 05:09:54",
            "blobId": "",
            "messageCount": 0,
            "patchEventVideoURL": "",
            "deviceName": "Intercom"
        }
    ]
}

I get all the response but i am not getting this "{\"hp\":\"6586129568\",\"camID\":\"camID120\",\"device_id\":38,\"pairedDeviceId\":\"204\"}" And get error like this -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1c5cb310 2013-11-29 12:37:33.280 Intercom[14720:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1c5cb310' * First throw call stack: Thank you

3
Are getting an error on reading the values from NSDictionary?Vinay Jain
json result is an arrayNeil Galiaskarov
Already several duplictes of this question.user529758
And use objectForKey:, not valueForKey: to access dictionary values unless you have specific reasons not to do so. - In this case valueForKey: hides the real problem, as it "distributes* over arrays.Martin R

3 Answers

1
votes

try this ...

-(void)requstJson1:(ASIHTTPRequest *)request
{
    jsonResponse = [request responseString];

    jsonDic = [jsonResponse JSONValue];

    NSString * jsonResult = [[jsonDic valueForKey:@"events"] retain];

    NSString * type=[[jsonResult valueForKey:@"time"] retain];

    NSArray * array=[[jsonResult valueForKey:@"data"] retain];

    NSDictionary * data = [array objectAtIndex:0];

    NSString * hp = [data objectForKey:@"hp"];

    NSString * camID = [data objectForKey:@"camID120"];

    NSString * device_id = [NSString stringWithFormat:@"%d",[data valueForKey:@"device_id"]];

    ...........
}
0
votes

If you can control the JSON output, you should definitely avoid JSON as string in JSON. You can nest JSON as deep as you want.

The problem is that the string in your data key is not JSON. It's escaped, so it cannot be read.

You should use NSJSONSerialization or any similar JSON parser. NSJSONSerialization is integrated in iOS, so you should try it:

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[[response responseString] dataUsingEncoding:NSUTF8StringEncoding]];

You can also use the response data directly, but I don't know ASIHTTP very well.

0
votes

Sorry for that trouble the JSON is not properly formatted the correct example is :

{
    "result": "ok",
    "totalcount": 422,
    "events": [
        {
            "id": "52982168e4b00e53abdace66",
            "deviceId": "203",
            "channelId": "",
            "data": {\"hp\":\"6586129568\",\"camID\":\"camID120\",\"device_id\":38,\"pairedDeviceId\":\"204\"},
            "longitude": 103.82,
            "latitude": 1.352,
            "type": "event-intercom-visitor-alert",
            "time": "29/11/2013 05:09:54",
            "blobId": "",
            "messageCount": 0,
            "patchEventVideoURL": "",
            "deviceName": "Intercom"
        }
    ]
}