0

I have a string [{"Latitude":8.55701,"Longitude":76.880934},{"Latitude":8.55701,"Longitude":76.880935},{"Latitude":8.55701,"Longitude":76.880935}]

I want output as [{8.55701,76.880934},..etc] only numbers.

This is a JSON string -

<script type="text/javascript">
 var myVar = ' <%= request.getAttribute("Map") %>';    /* retrieve json from request attribute */
 var result = myVar.split(',',2);
 var latitude = parseFloat(result[0].replace('"Latitude":',''));
 var longitude = parseFloat(result[1].split(':'));
 alert(latitude);  
 </script>

I have tried but not getting it.

  • Did you even try? Have you check the `split()` method? `JSON.parse` etc..? What have you tried? SO is not a free mechanical turd. – abrunet Nov 17 '15 at 08:17
  • Why not use `eval()` or `JSON.parse()`? – StackSlave Nov 17 '15 at 08:19
  • You have to know that, objects are key-value pairs, you can't just store them by themselves. so you'd have to then use an array instead. `[[8.55701,76.880934],..etc] to keep them in pairs if you want them to be. Alternatively you could just ave one array where every other number is a longitude, and every other other number is a latitude. – Sean_A91 Nov 17 '15 at 08:20
  • Possible duplicate of [How do I split a string with multiple separators in javascript?](http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – Pavel V. Nov 17 '15 at 08:27

4 Answers4

2

Use simple Regular expression to remove the strings:

.replace(/\"\w+\":/g, '');

'[{"Latitude":8.55701,"Longitude":76.880934},{"Latitude":8.55701,"Longitude":76.880935},{"Latitude":8.55701,"Longitude":76.880935}]'.replace(/\"\w+\":/g, '');

And if you want to get the values you can use JSON.parse:

var coords = JSON.parse('[{"Latitude":8.55701,"Longitude":76.880934},{"Latitude":8.55701,"Longitude":76.880935},{"Latitude":8.55701,"Longitude":76.880935}]');

// And loop
coords.forEach(function(coord) {
  console.log('latitude', coord.Latitude);
  console.log('longitude', coord.Longitude);
});
1

try this, this function will return a string as you expected out put.

function pareseJSONStr(str){

    var json = JSON.parse(str);
    var rslt = [];
    json.forEach(function(obj){
        rslt.push("{" + obj.Latitude + ", " + obj.Longitude + "}");
    });
    return "[" + rslt.join(",") + "]"
}

call this function as

var mystr = '[{"Latitude":8.55701,"Longitude":76.880934},{"Latitude":8.55701,"Longitude":76.880935},{"Latitude":8.55701,"Longitude":76.880935}]';
pareseStr(mystr);

returns a string

"[{8.55701, 76.880934},{8.55701, 76.880935},{8.55701, 76.880935}]"
Azad
  • 5,144
  • 4
  • 28
  • 56
0

If you really want your strange data format just use it:

var convertStrange = function( string ) {
    return '[' + JSON.parse( string ).map( function( item ) {
        return '{' + [ item.Latitude, item.Longitude ].join( ',' ) + '}';
    } ).join( ',' ) + ']';
};

So,

convertStrange('[{"Latitude":8.55701,"Longitude":76.880934},{"Latitude":8.55701,"Longitude":76.880935},{"Latitude":8.55701,"Longitude":76.880935}]')

will return string

[{8.55701,76.880934},{8.55701,76.880935},{8.55701,76.880935}]
Legotin
  • 2,378
  • 1
  • 18
  • 28
0
{8.55701,76.880934} 

is wrong object construction. Objects must be name:value pairs so it can only be

{name1:8.55701, name2:76.880934}

or you can use an array like

[[8.55701, 76.880934], ... ]

if you want it as a string use this;

var string = '[{"Latitude":8.55701,"Longitude":76.880934}, {"Latitude":8.55701,"Longitude":76.880935}, {"Latitude":8.55701,"Longitude":76.880935}]';
var newString = string.replace(/\"Latitude\":|\"Longitude\":/g, "");
yılmaz
  • 365
  • 1
  • 14