From this data
[{"lat":"-1.325416","lng":"36.669051"},
{"lat":"-1.392932","lng":"36.768752"},
{"lat":"-1.390505","lng":"36.810023"},
{"lat":"-1.448266","lng":"36.952769"},
{"lat":"-1.267033","lng":"37.094882"},
{"lat":"-1.214605","lng":"37.053978"},
{"lat":"-1.169516","lng":"36.895608"}]
I am trying to create a javascript object that looks like this.
var outerCoords =[
{lat: -1.325416, lng: 36.669051},
{lat: -1.392932, lng: 36.768752},
{lat: -1.390505, lng: 36.810023},
{lat: -1.448266, lng: 36.952769},
{lat: -1.267033, lng: 37.094882},
{lat: -1.214605, lng: 37.053978},
{lat: -1.169516, lng: 36.895608},
{lat: -1.244058, lng: 36.730391}
],
the property value without double quotes. I have first, stringified my json to get a string, then removed the double quotes from the string, then parsed the result with no double quotes. Parsing the result does not create an object, it returns a string. Please if you can help i'll appreciate. This is what am doing.
var str= JSON.stringify(outercords1);
var x = str.replace (/"/g,'');
var obj= JSON.parse(x);
the value of outercords is:
[{"lat":"-1.325416","lng":"36.669051"},{"lat":"-1.392932","lng":"36.768752"},{"lat":"-1.390505","lng":"36.810023"},{"lat":"-1.448266","lng":"36.952769"},{"lat":"-1.267033","lng":"37.094882"},{"lat":"-1.214605","lng":"37.053978"},{"lat":"-1.169516","lng":"36.895608"}]
Stringified JSON has quoted properties. This is mandatory. So a javascript object in the form
var outerCoords =[
{lat: -1.325416, lng: 36.669051},
{lat: -1.392932, lng: 36.768752},
{lat: -1.390505, lng: 36.810023},
{lat: -1.448266, lng: 36.952769},
{lat: -1.267033, lng: 37.094882},
{lat: -1.214605, lng: 37.053978},
{lat: -1.169516, lng: 36.895608},
{lat: -1.244058, lng: 36.730391}
],
Will be stringified as
'[{"lat":-1.325416,"lng":36.669051},{"lat":-1.392932,"lng":36.768752},{"lat":-1.390505,"lng":36.810023},{"lat":-1.448266,"lng":36.952769},{"lat":-1.267033,"lng":37.094882},{"lat":-1.214605,"lng":37.053978},{"lat":-1.169516,"lng":36.895608},{"lat":-1.244058,"lng":36.730391}]'
removing the quotes will turn it invalid to be parsed as JSON, so that's why you're getting a string.
On the other hand, performing
JSON.parse('[{"lat":-1.325416,"lng":36.669051},{"lat":-1.392932,"lng":36.768752},{"lat":-1.390505,"lng":36.810023},{"lat":-1.448266,"lng":36.952769},{"lat":-1.267033,"lng":37.094882},{"lat":-1.214605,"lng":37.053978},{"lat":-1.169516,"lng":36.895608},{"lat":-1.244058,"lng":36.730391}]');
Will give you an object. The same object you hand when you begun.
Object properties are always casted as strings, so declaring your object as
var outerCoords =[
{"lat": -1.325416, "lng": 36.669051},
{"lat": -1.392932, "lng": 36.768752},
{"lat": -1.390505, "lng": 36.810023},
{"lat": -1.448266, "lng": 36.952769},
{"lat": -1.267033, "lng": 37.094882},
{"lat": -1.214605, "lng": 37.053978},
{"lat": -1.169516, "lng": 36.895608},
{"lat": -1.244058, "lng": 36.730391}
];
is the same as declaring it without quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With