Can someone help me figure out how to correctly 'return' this JSON object in JavaScript?
function getJSONData() {
var points = '{\"points\": [';
var params = polyline.getLatLngs();
var i;
for (i = 0; i < points.length; i++) {
params = params.concat('{\"latitude\": ', points[i].lat, ', \"longitude\": ', points[i].lng, '}');
if (i < points.length - 1) {
params = params.concat(', ');
}
}
params = params.concat('] }');
return JSON.parse(params);
}
I am looking to grab this data using something like the following Objective-C code:
NSString *jsonDataString = [self.webView stringByEvaluatingJavaScriptFromString:@"getJSONData();"];
However, I need the data to be in NSData format instead of NSString. How can I achieve this conversion properly in Objective-C?
NSData *jsonData = [jsonDataString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONWritingPrettyPrinted error:&error];
Any suggestions on how to achieve this conversion from JSON as desired?