function parse_json()
{
var data = '{"name":"value", "array_names":["Violet","June"], "key_array": {"red":"opens back door","blue":"opens front door"} }'.evalJSON();
//json is the shiznit
alert(data.name);
alert(data.array_names[1]);
alert(data.key_array['blue']);
}
Sample Prototype Ajax Function using JSON
Javascript:
function php_json()
{
var url = 'json';
var id = 0;
var arrParams =
{
id: id,
}
var hash = $H(arrParams);
var params = hash.toQueryString();
var myAjax = new Ajax.Request(url,
{
parameters: params,
//$('id_of_form_element').serialize(true),
method:'post',
onSuccess: function(transport)
{
strResponse = transport.responseText || "empty";
strResponse = strResponse.replace(/[\r\n]+/g, "");
if(strResponse != 'empty')
{
data = strResponse.evalJSON();
alert(data.key_array.red);
}
else
{
alert('strResponse is empty');
}
},
onFailure: function()
{
alert('Something went wrong with changing the canceled status');
}
});
}
php:
public function json()
{
$this->render = false;
$red = 'opens side door';
$id = $_POST['id'];
$json=END needs 3 < before END though, won't allow this in the editor
{
"name":"value",
"array_names":["Violet","June"],
"key_array": {
"red":"{$red} {$id}",
"blue":"opens front door"
}
}
END;
echo $json;
}