3

I am creating a JSONArray and parse it to a String, but as it even contains Strings instead of code it doesn't output as I need it.

for(Place place: places){
    locations.put("new google.maps.LatLng("+place.getContactData().getLatitude()+","+place.getContactData().getLongitude()+")");
}
return locations.toString();

It outputs as: ["new google.maps.LatLng(53.5608,9.96357)","new google.maps.LatLng(53.5608,9.96357)"] but I need it without quotation marks like [new google.maps.LatLng(53.5608,9.96357),new google.maps.LatLng(53.5608,9.96357)] to be correctly interpreted by javascript.

3
  • 2
    And [new google.maps.LatLng(53.5608,9.96357),new google.maps.LatLng(53.5608,9.96357)] is going to be a string inside JSON itself or not? Because if not, then it's invalid JSON. Commented Feb 25, 2013 at 11:40
  • There is no code in a JSON data structure, thats simply not supported by the format. Commented Feb 25, 2013 at 11:41
  • OK, I get it. Thank you! Commented Feb 25, 2013 at 11:43

4 Answers 4

2

Another method would be:

create an array with just the coordinates:

for(Place place: places){
    JSONObject obj = new JSONObject();
    obj.put("lat",place.getContactData().getLatitude());
    obj.put("lng",place.getContactData().getLongitude());
    locations.put(obj);
}

and then in javascript:

var places = (yourPlacesJson);
var placeObjects = [];

for(var i=0;i<places.length;i++)
{
    placeObjects[placeObjects.length] = new google.maps.LatLng(places[i].lat,places[i].lng);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sounds really smart. In the source code the array shows up as var places = [{"lng":9.963569641113281,"lat":53.56079864501953},{"lng":9.979949951171875,"lat":53.55149841308594}]; but when I alert(places) it is empty. What might I have missed?
@Lester: There is not enough information to answer this question. Maybe you call alert(places) where places is not visible. Note that if you get the output [object Object],[object Object], that's perfectly normal.
Now it works perfectly. Maybe just some browser cache issue. :) Thanks a lot for all replies!
1

JSON only supports plain-old-data. It can't include any executable code (a new is executable code). This is by design - when JSON would be able to include executable code you would have to be much more carefully with importing JSON from an untrusted source.

All you can do is pass javascript code as strings and eval() it on the JS side after parsing the JSON.

Comments

0

Also you could use Regular expressions to remove the ", if you parse the json to another language

Comments

-1

i had a similar problem, the way i made this work:

instead of writing the javascript before the json conversion, insert a placeholder.

locations.put("%mapsPlaceholder1%");

then after filling the array with placeholders, do:

locations.toString().replaceFirst("\"%mapsPlaceholder1%\"","yourJsCode");

something like that

you could also just create the array string manually

4 Comments

Yeah, that's a nice approach. I just can't think of a way to do this for the whole List places.
i think an actually better way for your problem would be: just add the coordinates to the array, and then create the google.maps.LatLng object in javascript by iterating through the array
sounds like a fine plan b. Thank you!
look my 2nd answer, thats how i would do it

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.