I am currently trying to work with json and objective-c however having a bit of difficulty. The following is the json that is being returned
{
sethostname = (
{
msgs = "Updating Apache configuration\nUpdating cPanel license...Done. Update succeeded.\nBuilding global cache for cpanel...Done";
status = 1;
statusmsg = "Hostname Changed to: a.host.name.com";
warns = (
);
});
}
I am able to check that the response is coming back and the key is sethostname however no matter what I try I cannot get for example the value of status or statusmsg. Can anyone point me in the right location. The following is basic code I am using to check that sethostname is returned.
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&myError];
NSLog([res description]);
NSArray *arr;
arr = [res allKeys];
if ([arr containsObject:@"sethostname"])
{
NSLog(@"worked");
}
Why not use the simplest JSON method -
[myString jsonValue];
It's part of this JSON framework for objective-c
When in doubt, write down the structure of your JSON data. For example:
(which is in NeXTSTEP property list format, actually) means that you have a top-level dictionary. This top-level dictionary contains a key called
sethostname
whose value is an array. This array is comprised of dictionaries, each dictionary having a set of keys:msgs, status, statusmsg, warns
.msgs
has a string value,status
has a number value,statusmsg
has a string value,
warns` has an array value:Having understood this structure, your code should look like:
I don't think
if ([arr containsObject:@"sethostname"])
is going to work, because the results array is not going to contain that exact object. It might contain an object with the same content, but it won't be the SAME object.As jtbandes wrote, you need to log the actually output. NSLog both res and arr and see what you have.