Question

This is the code i have so far

// Parse data using NSJSONSerialization
NSError *error = nil;
NSArray *JsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error: &error];
if(!JsonArray)
{
    NSLog(@"Error Parsing Data: %@", error);
}
else
{
    for(NSDictionary *event in JsonArray)
    {
        if([[event description] isEqualToString:@"error"])
        {
            // Get error number? I am confused by this part
            NSLog(@"Element: %@", [event objectForKey:@"error"]);
        }
        else
        {
            NSLog(@"Element: %@", [event description]);
        }
    }
}

this is the JSON Data that parses correctly:

[{data string}, {data strings}]

This only gives me the string "error" and not the int as well:

{"error":0}

I am echoing this data from a PHP script if that helps any. Am i just doing it wrong, or did i miss something?

Was it helpful?

Solution

Your problem is that when you receive an error, you get back an NSDictionary and not an NSArray. This should work:

if ([jsonObject isKindOfClass:[NSArray class]]) {
    // no error: enumerate objects as you described above
} else if ([jsonObject isKindOfClass:[NSDictionary class]]) {
    // error: obtain error code
    NSNumber *errCode = jsonObject[@"error"];
} else {
    // something bad's happening
}

Stylistic pieces of advice:

  1. Don't call your object JsonArray, since it's not always an array. Call it jsonObject.

  2. Don't start variable names with capital letters.

OTHER TIPS

Would be great if you had posted the complete JSON document that you are trying to parse, because without that, there is absolutely no chance to figure out whether your code is anywhere near correct. The example [{data string}, {data strings}] that you gave is most definitely not a correct JSON document, so trying to parse it will return nil. {"error":0} is a dictionary with a single key "error" and a value 0. Having dictionaries with a single key is let's say unusual.

A JSON document contains either an array or object (using JSON terms) which will be turned either into an NSArray* or an NSDictionary*. You should know whether you expect an array or dictionary. If you expect an NSArray, check that [jsonObject isKindOfClass:[NSArray class]]. If you expect an NSDictionary, check that [jsonObject isKindOfClass:[NSDictionary class]]. If you don't do that then the wrong JSON document will either crash your app or produce total nonsense.

If you have an array then you will usually iterate through the elements of the array and handle each one in turn. If you have a dictionary you will usually look up keys that you know how to handle. What you are doing, iterating through an array of dictionaries, and checking for a dictionary with a key of "error", that's a very strange design of your JSON document.

And lookup what the "description" method does. "description" is what NSLog calls to find out what to print when it is asked to print an object. For an NSDictionary with a single key "error" and a value 0, it would return something like "error:0" which is of course not the same as "error".

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"jsonDic: %@", [jsonDic objectForKey:@"string"]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top