4

I want to parse the json output resulting from the following url in SBJSON framework for iOS http://maps.google.com/maps?q=school&mrt=yp&sll=13.006389,80.2575&output=json

while(1);{title:"school - Google Maps",url:"/maps?q=school\x26mrt=yp\x26sll=13.006389,80.2575\x26ie=UTF8\x26hq=school\x26hnear=",urlViewport:false,ei:"RCu3T4eeMqSiiAe7k-yZDQ",form:{selected:"q",q:{q:"school",mrt:"yp",what:"school",near:""},d:{saddr:"",daddr:"",dfaddr:""},geocode:""},

I am using http://www.bodurov.com/JsonFormatter/ to read it online.

In ASIHttpRequest response method I removed while(1); from the response

NSString *responseString = [[request resonseString]substringFromIndex:9]; //to remove while(1)
SBJSONParser * parser = [[SBJSONParser alloc]init];
NSDictionary *jsonDict = (NSDictionary*)[parser objectFromString:responseString];
NSLog(@"%@",jsonDict) // prints null
// [responseString JSONValue] also reports error

I guess JSON key without double quotes is causing problem.

Instead of { "title": "hospital - Google Maps", "urlViewport": false, }, we get { title: "hospital - Google Maps", "urlViewport": false }

Please help me to parse this complex JSON structure returned from Google.

Firnaz
  • 553
  • 7
  • 31
  • I want to parse a JSON representation which has no double quotes in its key. – Firnaz May 19 '12 at 10:34
  • Wow, that is broken! **BROKEN**!! The response starts off with `while(1);`, that's invalid JSON right there. I don't expect any JSON parser to be able to handle that. See [JSON spec](http://www.json.org/), for how JSON should be formatted. – Jeffery Thomas May 19 '12 at 16:05
  • Assuming that's not simply a dump of an NSDictionary (and it doesn't appear to be that) then it's broken -- it's simply not JSON. – Hot Licks Sep 05 '14 at 21:51
  • @JefferyThomas - The `while(1);` is a classic bit of protection against certain security exposures. It's expected to be removed on the receiving end. The rest of the "JSON" is badly broken, however. – Hot Licks Sep 05 '14 at 21:53
  • I can't believe that Google would be sending out such bad data. You must have a bad parameter or some such. – Hot Licks Sep 05 '14 at 21:56

2 Answers2

1

This worked better for my case because my values contained times which caused the regular expression in the above answer to match incorrectly.

json = [json stringByReplacingOccurrencesOfString: @"(\\w*[A-Za-z]\\w*)\\s*:"
                                       withString: @"\"$1\":"
                                          options: NSRegularExpressionSearch
                                            range: NSMakeRange(0, json.length)];
greg
  • 1,926
  • 17
  • 26
  • In general, agreed. For specific cases where you know the format of the data (or handle error checking in another way) this reg ex is a much shorter solution than working through character by character to recognize the malformed keys. – greg Sep 05 '14 at 23:00
  • If I had to, for a "real" environment, I'd rather take an open-source JSON parser and modify it to handle this. – Hot Licks Sep 05 '14 at 23:02
0

You need to add the missing quotes to the keys, so try this:

responseString = [responseString stringByReplacingOccurrencesOfString:@"(\\w+)\\s*:" 
                                 withString:@"\"$1\":" 
                                 options:NSRegularExpressionSearch 
                                 range:NSMakeRange(0, [responseString length])];

This should work well with the given JSON string.

Hejazi
  • 16,587
  • 9
  • 52
  • 67
  • Above given JSON is just an two line extract from the [link](http://maps.google.com/maps?q=school&mrt=yp&sll=13.006389,80.2575&output=json). I also tried this solution [link](http://www.dimzzy.com/blog/2011/09/fixing-invalid-json-before-parsing/). But no hope, better I can go with kml output. Thanks for your interest. – Firnaz May 19 '12 at 12:59