What's json syntax for {"data":"w-file1","attr":{"rel":"file"}} in php?
What would be php code to get this json?
开发者_StackOverflow社区{ "data": "w-file1",
"attr": { "rel" : "file"}
}
I am getting PHP Parse error: syntax error, unexpected T_DOUBLE_ARROW
error for
$file = ("data" => "w-file1","attr" => ("rel" => "file"));
echo json_encode($file);
$file needs to be an array as does the attr
key:
$file = array("data" => "w-file1","attr" => array("rel" => "file"));
echo json_encode($file);
Of course, you could inline it:
echo json_encode(array("data" => "w-file1","attr" => array("rel" => "file")));
Or, there is the OOP approach:
$file = new stdClass();
$file->data = 'w-file';
$file->attr = new stdClass();
$file->attr->rel = "file";
echo json_encode($file);
You need to specify array
for $file and attr:
$file = array("data" => "w-file1","attr" => array("rel" => "file"));
echo json_encode($file);
$file = array(
"data" => "w-file1",
"attr" => array("rel" => "file")
);
echo json_encode($file);
精彩评论