JIT为组织结构图构建功能性json

时间:2015-10-28 15:17:53

标签: php mysql json

我想构建JIT SpaceTree所需的json请求。

工作流程:以存储的回顾性答案的ID开始提问。

  • 如果选项为yes,则根据load_yes值加载下一个问题。此load_yes值选择id并加载该问题。

  • 如果选项为“否”,则按其存储在load_no下的ID加载下一个基于no的问题,

json应该是这样的:

var json = {
        id: "start",
        name: "does test work?",
        data: {},
        children: [{
            id: "layer1_1",
            name: "option: no, id 3, Q: test does work with option no?",
            data: {},
            children: []
        }, {
            id: "layer1_2",
            name: "option: yes, id 2, Q:  test does work!!",
            data: {},
            children: [{
                id: "layer2_1",
                name: "option: no, id 4, Q: test does work?",
                data: {},
                children: []
            }, {
                id: "layer2_2",
                name: "option: yes, id 5, Q: ",
                data: {},
                children: []
            }]
        }]
    };
$query = $mysqli->query("SELECT * FROM topic_answer");

while($obj = $query->fetch_object()){
    $arr[] = array(
        'id' => $obj->id,
        'name' => $obj->topic_question,
        'data' => '',
        'children' => array(array(
            'id' => $obj->load_yes,
            'name' => $obj->load_yes,
            'data' => '',
            'children' =>array(),
            ),array(
                'id' => $obj->load_no,
                'name' => $obj->load_no,
                'data' => '',
                'children' => array(),

            ),
        )
    );
     id, topic_name,  topic_creator, topic_question,  load_yes,  start,  load_no,   end 
     1    test        jordan       does test work?         2          1       3          0
     4    test        jordan       test does work no       0          0       0          0
     5    test        jordan       test does work yes      0          0       0          0
     2    test        jordan       test does work yes!!    4          0       5          0

1 个答案:

答案 0 :(得分:0)

在json中,对象被描述为HEAD。所以这个代码基本上意味着一个对象数组:

{}

所以这里,你在哪里嵌套数组:

[
  {},
  {}
]

您实际上想要用对象替换内部数组,例如stdClass()。 stdClass使用如下:

'children' => array(array(
    'id' => $obj->load_yes,
    'name' => $obj->load_yes,
    'data' => '',
    'children' =>array(),
    ),array(
        'id' => $obj->load_no,
        'name' => $obj->load_no,
        'data' => '',
        'children' => array(),
    ),
)

然后,用php

$obj = new stdClass();
$obj->id = "layer1_1";
$obj->name = "option: no, id 3, Q: test does work with option no?";
$obj->data = new stdClass();
$obj->children = array();
相关问题