Your string is not proper, JSON parser will never parse it properly. Here you can't use eval() function approach. It's not like an Array but a HashMap or simply malformed Object. In your case, You need json with key-value pair combination.
{'lat': -37.819616, 'lng': 144.968119},
{'lat': -38.330766, 'lng': 144.695692}
If Your Object does not contains child objects/arrays you can use the following code.
function formatJSON2Array (str) {
var arr = [];
str = str.replace(/^\{|\}$/g,'').split(',');
for(var i=0,cur,pair;cur=str[i];i++){
arr[i] = {};
pair = cur.split(':');
arr[i][pair[0]] = /^\d*$/.test(pair[1]) ? +pair[1] : pair[1];
}
console.log("JSON Array>>> " +JSON.stringify(arr)); // it will print [{"a":12},{"b":"c"},{"foo":"bar"}] in console
return arr;
}
formatJSON2Array("{lat: -37.819616, lng: 144.968119},{lat: -38.330766, lng: 144.695692}");
Above code will convert your entered string into Array of objects.
If however you actually wanted a HashMap (Associative Array) and NOT an array, use the following code:
function FormatJSON2Object(str) {
var obj = {};
str = str.replace(/^\{|\}$/g,'').split(',');
for(var i=0,cur,pair;cur=str[i];i++){
pair = cur.split(':');
obj[pair[0]] = /^\d*$/.test(pair[1]) ? +pair[1] : pair[1];
}
console.log("JSON Object >>> "+ JSON.stringify(obj)); // it will return {"lat":" -37.819616"," lng":" 144.695692","{lat":" -38.330766"}
return obj;
}
FormatJSON2Object("{lat: -37.819616, lng: 144.968119},{lat: -38.330766, lng: 144.695692}");
Please note that, above code will become a lot more complex when you start nesting objects and arrays with it. This thread helped me a lot for this JSON conversions with array-objects and parsing. If you have any key value pair except lat lng, it will work fine.