Here is my code:
$result = mysql_query("SELECT * FROM Product_Characteristics");
$charact_array = array();
while($row = mysql_fetch_array($result)) {
$charact_array[$row['PC_ID']] = $row['Title'];
}
$JSON_char = json_encode($charact_array);
echo $JSON_char;
The echoed response is something like this:
{"1":"product1","2":"product2","3":"product3","4":"product4"}
I am not sure how to access these values using the eval function in javascript once this data is returned…
Typically, using the eval function I can obtain the values of something like this:
{
"ptypes": {
"aptype": [
{
"id": "1",
"title": "product1"
},
{
"id": "2",
"title": "product2"
}
]
}
}
Using some code like this:
var JSONobject = eval('(' + msg + ')'); //msg is the JSON response from above
alert(JSONobject.ptypes.aptype.title[0]) // product1
I basically need to be able to access the values within the JSON (which I have encoded above from a PHP associative array) and loop through them.
If this is not possible, or if it is better to encapsulate my JSON data within the structures “ptypes” and “aptype” (which seems not necessary), I appreciate any advice.
If you are receiving an actual string, you can parse it using
What you really should be doing though is returning the correct content type
Then, you can use something like jQuery’s
getJSON()function