0

I have the next php script:

<?PHP
$results = array(
    array(
        'col1'=>'val1.1',
        'col2'=>'val1.2'
    ),
    array(
        'col1'=>'val2.1',
        'col2'=>'val2.2'
    )
);
echo json_encode($results);

?>

Every array from the main array represents a raw from a database. The results variable is sent to an OSx app. I'm trying to decode the json string to sync a local database with a remote database.

I'm using this code:

NSError *error = nil;
id object = [NSJSONSerialization
                     JSONObjectWithData:receivedData
                     options:0
                     error:&error];

if(error) { /* JSON was malformed, act appropriately here */ }
if([object isKindOfClass:[NSDictionary class]]){
  NSDictionary *results = object;       
  NSLog(@"%@",results);
}else{
  NSLog(@"there is not an JSON object");
}

The problem is the app is not recognise the received string as an object. Can some one explain me why and what I'm doing wrong?

The string received is:

[{"col1":"val1.1","col2":"val1.2"},{"col1":"val2.1","col2":"val2.2"}]
user1792771
  • 99
  • 13
  • 1
    Duplicate of [How to use NSJSONSerialization](http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization). Your object is an array of dictionaries, not a dictionary. – Nate May 26 '13 at 22:13
  • 1
    Go to json.org and spend 5-10 minutes studying the syntax diagram. That's all it takes to learn the language, and then you won't have to puzzle over what you have anymore. – Hot Licks May 27 '13 at 00:31
  • just add `header('Content-type: application/json');` this line to your PHP code before `echo json_encode($results);` this line, and it will send json response back, and in response first object is an `Array` not `Dictionary` so read array then dictionary. – Dipen Panchasara May 27 '13 at 04:12

0 Answers0