Steve Steve - 1 year ago 77
Javascript Question

Parse JSON array with JavaScript

I have a MySQL database with a table of Latitudes and longitudes with associated data. I'm trying to create a Google Map and want to parse the JSON string using JavaScript. I have looked here and on YouTube and I don't know how to solve what I'm doing wrong.

Here is my JSON string that I get with my echo statement:

{"cm_mapTABLE":[["1","Angels Rest Photos OR","Angels_Rest_Photos_OR","663","aaj.jpg","2","Angel's Rest","Hike one of the trails in the Gorge with a scenic overlook, stream, and waterfall.","0","blue","4.5","45.5603","-122.171","http:\/\/\/pgallery\/index.php?\/category\/6","Angels_Rest_Photos_OR\/aae.thumb.jpg\" ALIGN=left HEIGHT=115 WIDTH=150>","http:\/\/\/pgallery\/i.php?\/galleries\/Angels_Rest_Photos_OR\/aaj-th.jpg","","","",""],["2","Ape Canyon Photos WA","Ape_Canyon_Photos_WA","681","PICT0114.jpg","3","Ape Canyon Trail","This trail is popular with hikers and mountain bikers with great views of several mountains.","0","blue","11","46.1653","-122.092","http:\/\/\/pgallery\/index.php?\/category\/8","Ape_Canyon_Photos_WA\/PICT0114.thumb.jpg\" ALIGN=left HEIGHT=115 WIDTH=150>","http:\/\/\/pgallery\/i.php?\/galleries\/Ape_Canyon_Photos_WA\/PICT0114-th.jpg","","","",""]]}

Here is my code:

<!DOCTYPE html>
require("phpsqlsearch_dbinfo.php"); // This is the file with your logon info

//$host="localhost"; //replace with your hostname
//$username="root"; //replace with your username
//$password=""; //replace with your password
//$db_name="$database"; //replace with your database
$con=mysqli_connect("$hostname", "$username", "$password", "$database")or die("cannot connect");
//mysqli_select_db("$db_name")or die("cannot select DB");
$sql = "select * from location_markers WHERE mrk_id < 3"; //replace emp_info with your table name
$result = mysqli_query($con, $sql);
$json_string_data = array();

// You have to give the json a name to make it accessible by JS, e.g.:
// echo 'file='.json_encode($json_string_data),';';

// This statement will echo the json output in the displayed web page
echo json_encode($json_string_data);
// please refer to our PHP JSON encode function tutorial for learning json_encode function in detail
for (i = 0;i < $json_string_data.length;i++){
var javascript_string_data=<?php echo json_encode($json_string_data); ?>;

I'm using Firebug and here is the error:

ReferenceError: $json_string_data is not defined

for (i = 0;i < $json_string_data.length;i++){

Could someone please tell me the proper way to refer to elements in my JSON string? Do I need to somehow get the field names in my JSON string?

Answer Source

var javascript_string_data=<?php echo json_encode($json_string_data); ?>; is forming a variable named javascript_string_data containing an object.

You are trying to access cm_mapTABLE.rank[i] which is actually considered as a property of the window object.

Try to output


Be aware that assigning a JSON string directly could result in security issues when relying on external data previously stored into database. Use the JSON.parse() method instead.

Further you are trying to use a PHP variable in your JavaScript loop.

Define your JavaScript variable before using it in a loop:

var javascript_string_data = JSON.parse('<?echo json_encode($json_string_data); ?>');
for (i = 0 ; i < json_string_data.length ; i++){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download