How to convert json encoded PHP array to array in Javascript?

I am extracting a JSON-encoded array using AJAX from a PHP file, but in JavaScript I need to use it as an array, how can I create an array in Javascript?

My AJAX call to a PHP file:

$.ajax({ type:"POST", url:"ajaxfetch.php", success:function(result){ alert(result); } }); 

ARRAY Created in PHP file:

 Array ( [0] => Array ( [id] => 4 [deviceID] => xyz123 [latitude] => -33.80010128657071 [longitude] => 151.28747820854187 [altitude] => 34.78788787 [createdDate] => 2013-08-15 23:00:00 [delete] => 0 ) [1] => Array ( [id] => 5 [deviceID] => jdkfhjskh344 [latitude] => -33.950198 [longitude] => 151.259302 [altitude] => 76.44455 [createdDate] => 2013-08-15 21:50:42 [delete] => 0 ) [2] => Array ( [id] => 1 [deviceID] => abc223 [latitude] => -33.890542 [longitude] => 151.274856 [altitude] => 21.4454455 [createdDate] => 2013-08-15 20:00:00 [delete] => 0 ) ) 

I json encoded this array in PHP , but AJAX extracted it and output it to a string .

ABOVE ARRAY json encode as below:

 $data = array(); $data = $locate_obj->getAllDeviceLocation(); echo json_encode($data); 

Json_encode output

 [{"id":"4","deviceID":"xyz123","latitude":" -33.80010128657071","longitude":"151.28747820854187","altitude":"34.78788787","createdDate":"2013-08-15 23:00:00","delete":"0"},{"id":"5","deviceID":"jdkfhjskh344","latitude":"-33.950198","longitude":"151.259302","altitude":"76.44455","createdDate":"2013-08-15 21:50:42","delete":"0"},{"id":"1","deviceID":"abc223","latitude":"-33.890542","longitude":"151.274856","altitude":"21.4454455","createdDate":"2013-08-15 20:00:00","delete":"0"}] 

I am looking for a way to create an array in Javascript with the output that I get in the ajax response so that I can come up with an array of format:

 var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; 
+7
json javascript arrays ajax php
source share
4 answers

There are three solutions to this problem:

  • Call JSON.parse explicitly and pass the response text to it . The return value will be the JavaScript data type.

  • Set dataType: 'json' parameter to $.ajax so jQuery $.ajax JSON for you.

  • Set the correct response headers for JSON in PHP. jQuery will determine the header and automatically parse JSON.

If you want to change the structure on the client side, look at access / process objects (nested), arrays, or JSON .

+10
source share
 http://api.jquery.com/jQuery.getJSON/ $.getJSON('ajaxfetch.php', function(data) { var locations = []; $.each(data, function(key, val) { locations[val.deviceID] = []; locations[val.deviceID].push(val.id); locations[val.deviceID].push(val.latitude); locations[val.deviceID].push(val.longitude); }); }); 

It is checked not on 100%, but on the correct lines. Not sure where you get the location name, since it is not in an array, so I used deviceID. Using getJSON should make your life easier.

+4
source share

Make sure your output is valid JSON, then specify dataType: "json" in your AJAX jQuery request:

 $.ajax({ type: "POST", url: "ajaxfetch.php", dataType: "json", success: function (result) { console.log(result); //Now a JSON object } }); 
0
source share
 var locations = []; $.ajax({ type: "POST", url: "ajaxfetch.php", dataType: "json", success: function (result) { result.forEach(function(loc) { locations.push(new Array(loc.deviceID, loc.latitude, loc.longitude, loc.id)) }); // locations is now in desired format (except non-existent place name) } }); 
-one
source share

All Articles