How to create a JSONArray that contains Javascript code inside Java? How to create a JSONArray that contains Javascript code inside Java? json json

How to create a JSONArray that contains Javascript code inside Java?


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);}


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.


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