How to parse JSON without quotes with my KEY string?

I want to analyze the json output resulting from the following URL in the 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 use http://www.bodurov.com/JsonFormatter/ to read it online.

In the ASIHttpRequest response method, I deleted while (1); from the answer

 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 think that a JSON key without double quotes is causing the problem.

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

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

+4
source share
2 answers

This worked better for my case, because my values ​​contained moments that caused the regular expression in the answer above to be incorrect.

 json = [json stringByReplacingOccurrencesOfString: @"(\\w*[A-Za-z]\\w*)\\s*:" withString: @"\"$1\":" options: NSRegularExpressionSearch range: NSMakeRange(0, json.length)]; 
+1
source

You need to add the missing quotation marks to the keys, so try the following:

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

This should work well with a given JSON string.

0
source

Source: https://habr.com/ru/post/1413291/


All Articles