我想输出这样的JSON:
[{"item":{"id":"id1","label":"Lorem ipsum dolor 1","checked":false},"children":[{"item":{"id":"id11","label":"Lorem ipsum dolor 11","checked":false}},{"item":{"id":"id12","label":"Lorem ipsum dolor 12","checked":false}},{"item":{"id":"id13","label":"Lorem ipsum dolor 13","checked":false}}]}]
我有以下PHP代码:
<?php
$section1['id'] = "id1"; $section1['label'] = "Lorem ipsum dolor 1";$section1['checked'] = "false"; $pie_chart = $section1;
$section10a['id'] = "id1"; $section10a['label'] = "Lorem ipsum dolor 11";$section10a['checked'] = "false"; $pie_chart1 = $section10a;
$section10b['id'] = "id2"; $section10b['label'] = "Lorem ipsum dolor 12";$section10b['checked'] = "false"; $pie_chart2 = $section10b;
$section10c['id'] = "id2";$section10c['label'] = "Lorem ipsum dolor 12";$section10c['checked'] = "false"; $pie_chart3 = $section10c;
$dashboard['item'] = $pie_chart; $dashboard['children'][] = $pie_chart1;$dashboard['children'][] = $pie_chart2; $dashboard['children'][] = $pie_chart3;
$output = $dashboard; echo json_encode($output);
?>
答案 0 :(得分:1)
试试这个:
首先,使用PHP,您可以创建一个具有此结构的数组,然后使用json_encode函数:
$array = array(
array(
"item" =>
array("id" => "id1", "label" => "Lorem ipsum dolor 1", "checked" => false),
"children" =>
array(
array("item"=>array("id" => "id11", "label" => "Lorem ipsum dolor 11", "checked" => false)),
array("item"=>array("id" => "id12", "label" => "Lorem ipsum dolor 12", "checked" => false)),
array("item"=>array("id" => "id13", "label" => "Lorem ipsum dolor 13", "checked" => false)),
)
)
);
$json_data = json_encode($array);
echo $json_data;
输出:
[{"item":{"id":"id1","label":"Lorem ipsum dolor 1","checked":false},"children":[{"item":{"id":"id11","label":"Lorem ipsum dolor 11","checked":false}},{"item":{"id":"id12","label":"Lorem ipsum dolor 12","checked":false}},{"item":{"id":"id13","label":"Lorem ipsum dolor 13","checked":false}}]}]
答案 1 :(得分:0)
你可以这样做
json_encode($json_variable);