2

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?

8
  • @H2CO3 sorry, thanks for the edit. Commented Nov 9, 2013 at 19:13
  • Apart from that: you want to examine if there is an object with the key error (i. e. if (event[@"error"] != nil)). Commented Nov 9, 2013 at 19:13
  • @H2CO3 I tried to use [event objectForKey ... but i got Sigabrt. Commented Nov 9, 2013 at 19:15
  • And what was the exception message? "I got SIGABRT" in itself is non-informative. Commented Nov 9, 2013 at 19:16
  • Sorry, let me paste it in the question Commented Nov 9, 2013 at 19:17

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

In your answer, could you explain why not to use capital letters? That would really help, let me try that.
@EliteGamer because it's a convention.
Could you explain what you mean by that?
@EliteGamer Names starting with caps are used for naming classes in Objective-C. If you don't adhere to this, you'll confuse the people who read your code.
Also, one more thing. Where do i place the code? When i rename your values with mine, i get an error. Expected Method to read dictionary element not found. @H2CO3
|
0

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".

Comments

0
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"jsonDic: %@", [jsonDic objectForKey:@"string"]);

4 Comments

can you try the above
@Parthi , he is correct. I will not have an error all the time.
Got it. You have mentioned about the error. Not gone through it !!
@Parthi that is fine. Basically, when there is not error, the first JSON will be echoed, but if there is one, i need to see that there is error string in the JSON, then get the error number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.