How to analyze json data with & ldquo; var abcd = & rdquo; on your mind


I'm having problem with parsing json data file on iOS. This is a sample from the data.json file:

var devs = [
"tech":"iOS, HTML5, CSS, RWD",
"tech":"Android, Java, Node.js",

and so on.

With "normal" json files I use:

NSURLResponse *response;
NSError *myError;
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL      URLWithString:@"http://somerailsapplication/posts.json"]     cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&myError];
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];

Unfortunately this solution doesn't work in this case. Is there any chance to get this working without searching for specific string "var dev=[" and the last "]" in the downloaded data?

The response is javascript, not JSON, so you won't be able to use a JSON parser directly. If you can't change the server output, the easiest thing would be to strip the beginning and end of the data, as you suggested. You could also embed the response in an HTML template and evaluate it in a webview, but that seems like a lot of more work.