用 php 编码 json 并解析
Enconding json with php and parsing
我需要为运行一个具有这种结构的js创建两个变量
var sets = [{"label":"PHP","size":"6"},{"label":"SQL","size":"1"}];
var overlaps = [ {"sets":[0,1],"size":"0"}];
我正在尝试用 php 动态地创建它,就像这样
$sets[] = array("label" =>"PHP", "size" => "6");
$overlaps[] = array("sets" => array(0,1), "size" => "0");
print json_encode(array($sets, $overlaps));
在ajax我这样做
$.post(action
, {param:param}
, function(returned_data){
console.log(returned_data);
var json = $.parseJSON(returned_data);
sets = json[0];
overlaps = json[1];
});
Console.log 转储这个
[[{"label":"PHP","size":"6"},{"label":"SQL","size":"1"},{"label":"JQuery","size":"1"}],[{"sets":[0,2],"size":"1"}]]
错误是“无法读取未定义的属性'push'”
怎么了?如何解析 json 并将每个部分分配给变量?
我认为您不需要将其解析为 JSON,控制台暗示它已经是 object。此外,return
几乎在所有语言中都是保留字(语言结构),因此您应该养成不将其用于变量名的习惯。
$.post(action
, {param:param}
, function(return_data){
return_data = typeof return_data=='object' ? return_data : $.parseJSON(return_data);
//The line above parses the string only if the browser didn't already recognize it as a JSON-object.
console.log(return_data);
sets = return_data[0];
overlaps = return_data[1];
});
此外,您应该在 PHP 中设置 header(那时肯定不需要 $.parseJSON
):
$sets[] = array("label" =>"PHP", "size" => "6");
$overlaps[] = array("sets" => array(0,1), "size" => "0");
header('Content-type: application/json'); //I added this line
echo json_encode(array($sets, $overlaps));
奇怪的错误。可能是因为您使用保留字作为变量。
jQuery $.post
已经期望 JSON 所以不需要 JSON 解析 .
$.post(action
, {param:param}
, function(returned_data){
console.log(returned_data);
var json = returned_data;
sets = json[0];
overlaps = json[1];
});
我需要为运行一个具有这种结构的js创建两个变量
var sets = [{"label":"PHP","size":"6"},{"label":"SQL","size":"1"}];
var overlaps = [ {"sets":[0,1],"size":"0"}];
我正在尝试用 php 动态地创建它,就像这样
$sets[] = array("label" =>"PHP", "size" => "6");
$overlaps[] = array("sets" => array(0,1), "size" => "0");
print json_encode(array($sets, $overlaps));
在ajax我这样做
$.post(action
, {param:param}
, function(returned_data){
console.log(returned_data);
var json = $.parseJSON(returned_data);
sets = json[0];
overlaps = json[1];
});
Console.log 转储这个
[[{"label":"PHP","size":"6"},{"label":"SQL","size":"1"},{"label":"JQuery","size":"1"}],[{"sets":[0,2],"size":"1"}]]
错误是“无法读取未定义的属性'push'”
怎么了?如何解析 json 并将每个部分分配给变量?
我认为您不需要将其解析为 JSON,控制台暗示它已经是 object。此外,return
几乎在所有语言中都是保留字(语言结构),因此您应该养成不将其用于变量名的习惯。
$.post(action
, {param:param}
, function(return_data){
return_data = typeof return_data=='object' ? return_data : $.parseJSON(return_data);
//The line above parses the string only if the browser didn't already recognize it as a JSON-object.
console.log(return_data);
sets = return_data[0];
overlaps = return_data[1];
});
此外,您应该在 PHP 中设置 header(那时肯定不需要 $.parseJSON
):
$sets[] = array("label" =>"PHP", "size" => "6");
$overlaps[] = array("sets" => array(0,1), "size" => "0");
header('Content-type: application/json'); //I added this line
echo json_encode(array($sets, $overlaps));
奇怪的错误。可能是因为您使用保留字作为变量。
jQuery $.post
已经期望 JSON 所以不需要 JSON 解析 .
$.post(action
, {param:param}
, function(returned_data){
console.log(returned_data);
var json = returned_data;
sets = json[0];
overlaps = json[1];
});