1

I've been bashing my head against a wall for a bit.

I have a rails back-end that is returning JSON to my iOS app. I'm using rails' default return for rendering my object in JSON for me automatically. I'm having trouble with the errors it returns.

The JSON I get for errors is {"errors":{"email":["can't be blank"],"password":["can't be blank"]}}.

I use ASI for handling the request.

-(void) requestFinished:(ASIFormDataRequest *)request {
  NSDictionary *data = [[request responseString] JSONValue];

Doing the above code make data become:

{
  errors =     
  {
    email =         (
        "can't be blank"
    );
    password =         (
        "can't be blank"
    );
  };
}

Now this gives me issues trying to parse it out. I'd like to be able to access each element of errors, and its associated value.

When I try to loop through the elements, I do something like:

for (NSDictionary *error in [data objectForKey:@"errors"])

This will give me email and password, but they are of type __NSCFString, not NSDictionary. I haven't been able to find a way to get the value for either email or password. Does anyone have an idea on how to parse this out?

Thanks!

2 Answers 2

1

This should work, note that response has the same structure than your 'data' NSDictionary.

NSDictionary *fields = [[NSDictionary alloc] initWithObjectsAndKeys: [[NSArray alloc] initWithObjects:@"one", @"two", nil],
                                                                @"A",
                                                                [[NSArray alloc] initWithObjects:@"three", @"four", nil],
                                                                @"B",
                                                                nil];

NSDictionary *response = [[NSDictionary alloc] initWithObjectsAndKeys:fields, @"errors", nil];

NSLog(@"Dictionary: %@", [response objectForKey:@"errors"]);


for (NSString *field in [response objectForKey:@"errors"])
    for (NSString* error in [response valueForKeyPath:[NSString stringWithFormat:@"errors.%@", field]])
        NSLog(@"%@ %@", field, error);

The output will look like this:

Dictionary: {
A =     (
    one,
    two
);
B =     (
    three,
    four
);
}

A one
A two
B three
B four
Sign up to request clarification or add additional context in comments.

Comments

1

Well i dont have Mac right now but i'm trying to help you if it doesnt work let me know i will correct it tomorrow.

-(void) requestFinished:(ASIFormDataRequest *)request 
 {
     NSArray *data = [[request responseString] JSONValue];
     NSDictionary *dict = [data objectAtIndex:0];

     NSDictionary *dict2 = [dict valueForKey:@"errors"];

     NSLog(@"email = %@, password = %@",[dict2 valueForKey:@"email"], [dict2 valueForKey:@"password"]);
 }

1 Comment

Thanks for trying without your mac handy. Unfortunately it won't work by using an NSArray. The accepted answer does work though, looks like valueForKeyPath was the only way to do this.

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.