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.

4
  • Did you even try? Have you check the split() method? JSON.parse etc..? What have you tried? SO is not a free mechanical turd. Commented Nov 17, 2015 at 8:17
  • Why not use eval() or JSON.parse()? Commented Nov 17, 2015 at 8: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. Commented Nov 17, 2015 at 8:20
  • Possible duplicate of How do I split a string with multiple separators in javascript? Commented Nov 17, 2015 at 8:27

4 Answers 4

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Vote up or mark as correct if it helps you. You're very welcome
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}]"

Comments

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}]

5 Comments

what if it's an ACTUAL string (looks like a REAL JSON string to me) - easy fix - JSON.parse the string first :D ... of course, then the output HAS to be a string ... because it's not valid anything else
It is a valid array, containing objects, nope?
I mean the output "required" in the question, your code is good man
I suppose it's a misprint or some like that, or he really wants invalid data format?
he wants a string maybe?
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, "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.