0

So I have a web service that returns this JSON:

[{"other_user":"54","distance":"1 ft","duration":"1 min"},{"other_user":"55","distance":"2 ft","duration":"5 min"}]

Then in my ios app I use:

NSString *responseString = [request responseString];
    NSArray *responseArray = [responseString JSONValue];
    for (NSDictionary* item in responseArray) {
        NSString *otherUser = [item objectForKey:@"other_user"];
        NSString *otherDistance = [item objectForKey:@"distance"];
        NSString *otherDuration = [item objectForKey:@"duration"];

       NSLog(@"user: %@  distance: %@  time: %@", otherUser, otherDistance, otherDuration);
    }

but I am getting this error:

-JSONValue failed. Error is: Illegal start of token [S]

Any help is appreciated, I have no Idea where to look/debug

4
  • 2
    Validate your JSON response (jsonlint.com) to see if its a valid JSON object. And Which JSON Parser are you using to parse the JSON response in your project ? Commented Feb 23, 2012 at 4:08
  • I am using SBJSON and it is valid Commented Feb 23, 2012 at 4:16
  • 1
    Print out responseString. I'm guessing it's not valid JSON. Commented Feb 23, 2012 at 4:28
  • You're right, I had some echos in there. I feel stupid now. Sorry for wasting your time. Commented Feb 23, 2012 at 4:37

1 Answer 1

1

try the following:

SBJSON *parser = [[SBJSON alloc] init];
NSString *responseString = [request responseString];

NSError *jsonError=nil;
NSArray *responseArray = [parser objectWithString: responseString error: &jsonError];

if (jsonError==nil) {
   for (NSDictionary* item in responseArray) {
      NSString *otherUser = [item objectForKey:@"other_user"];
      NSString *otherDistance = [item objectForKey:@"distance"];
      NSString *otherDuration = [item objectForKey:@"duration"];

      NSLog(@"user: %@  distance: %@  time: %@", otherUser, otherDistance, otherDuration);
   }
}

Good Luck

t

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

3 Comments

Tried this but apparently jsonError!=nil
then your json is probably invalid.
Yea, I guess I had some echos in the PHP that I didn't comment out after testing. which messed it up, it's working now. Thanks for your help

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.