I am trying to map traceroutes to google maps.

I have an array in php with traceroute data as

$c=ip,latitude,longitude, 2nd ip, its latitude, longitude, ....target ip, its lat, its lng

I used json_encode($c, JSON_FORCE_OBJECT) and saved the file

Now, how do I access this using javascript, by directly equating it to new JS object?

earlier I used to have a data format like this on harddrive

var data12 = {

"route":[
{
    "ip": "some ip",

    "longitude": "some lng",

    "latitude": "some lat",

.....

and in my javascript it was used as

data=data12.route;

and then simply acces the members as data[1].latitude

link|improve this question

feedback

2 Answers

up vote 3 down vote accepted

I recommend using the jQuery library. The minified version only has 31 kB in size and provides lots of useful functions.

For parsing JSON, simply do

var obj = jQuery.parseJSON ( ' {"name" : "John"} ' );

You can now access everything easily:

alert ( obj.name );

Note: jQuery uses the browser's native JSON parser - if available - which is very quick and much safer then using the eval () method.

Edit: To get data from the server side to the client side, there are two possibilities:

1.) Use an AJAX request (quite simple with jQuery):

   $.ajax ( {
       url: "yourscript.php",
       dataType: "json",
       success: function ( data, textStatus, jqXHR ) {
           // process the data, you only need the "data" argument
           // jQuery will automatically parse the JSON for you!
       }
   } );

2.) Write the JSON object into the Javascript source code at page generation:

   <?php
       $json = json_encode ( $your_array, JSON_FORCE_OBJECT );
   ?>

   <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>

   <script type="text/javascript">
   //<![CDATA[

   var json_obj = jQuery.parseJSON ( ' + <?php echo $json; ?> + ' );

   //]]>
   </script>
link|improve this answer
+1 for mentioning the native JSON parser – Igor Zinov'yev Aug 29 '11 at 10:53
How do I get my csv data to the jquery routines? does Php json_encode work? Also, what If I have more than one traceroutes to encode? – user494461 Aug 29 '11 at 10:54
You can simply use an AJAX request, if you want to load the data on a certain action or event. If you have access to the Javascript part in your web site for the PHP part, you could also write the JSON object into the Javascript file. Throw your array into PHP's json_encode () function like this: $json_obj = json_encode ( $your_array, JSON_FORCE_OBJECT );. – Sammy S. Aug 29 '11 at 10:59
feedback

Simply eval() the data in Javascript.

link|improve this answer
1  
Eval is evil. Use is not recommended! – Sammy S. Aug 29 '11 at 10:46
Generally, yes. However, this is the way cross-language communication via JSON works (processing an Ajax request, for instance, also requires an eval of the response). – Rijk Aug 29 '11 at 10:48
No, it does not require eval (). You can also use window.JSON.parse () if available which is much safer. Have a look at the jQuery function for JSON parsing ( code.jquery.com/jquery-1.6.2.js, search for "parseJSON" ). – Sammy S. Aug 29 '11 at 10:52
The main requirement for me is getting the data from php - csv style formatted data to JS. So eval seems to be a better fit. – user494461 Aug 29 '11 at 10:58
1  
If you do want to use eval, at the very least you must use json2.js. It runs a set of regexes first to avoid security issues. github.com/douglascrockford/JSON-js – Joeri Sebrechts Aug 29 '11 at 10:59
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.