使用子项迭代JSON对象

时间:2015-04-20 19:03:45

标签: php json laravel-5 fancytree

我正在使用fancytree通过ajax发送数据。

$("#save").click(function(e){
            e.preventDefault();

            var tree = $("#tree").fancytree("getTree").rootNode;
            var data = tree.toDict(true);
            data = JSON.stringify(data);
            $.ajax(
            { 
                type: "POST",
                data :  { "data": data },
                contentType: "application/json",
                success: function(data) {
                    $('#alert').empty().append(data);
                }
            });
        });

JSON数据是以下格式。我需要重申以下JSON对象以便操作数据。

{
"expanded":true,
"key":"root_1",
"selected":false,
"title":"root",
"children":
[
    {"key":"p_1","selected":false,"title":"Animal Meat"},
    {"key":"p_2","selected":false,"title":"Animal Product"},
    {"folder":true,"key":"p_3","selected":false,"title":"Beverage","children":  
    [
        {"key":"2","selected":false,"title":"Tea"},
        {"key":"3","selected":false,"title":"Coffee"},
        {"key":"4","selected":false,"title":"Water"}
    ]},
    {"folder":true,"key":"p_4","selected":true,"title":"Cereal","children": 
    [
        {"key":"6","selected":true,"title":"Porridge"}
    ]},
    {"folder":true,"key":"p_5","selected":false,"title":"Dairy Product","children":
    [
        {"key":"7","selected":false,"title":"Cheese"},
        {"key":"8","selected":false,"title":"Curd"},
        {"key":"10","selected":false,"title":"Milk"},
    ]}
]}

Laravel中的以下函数处理此请求。

public function storePreference(Request $request)
    {
        $data = $request->getContent();

        $data = urldecode($data);

        foreach($data['children'] as $child)
        {
           $key      = $child['key'];
            $selected = $child['selected'];
            $title    = $child['title'];

            printf("Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
        }
    }

我需要以下输出:

Key: p_1, selected: false, title: Animal Meat
Key: p_2, selected: false, title: Animal Product
Key: p_3, selected: false, title: Beverage
Key: 2, selected: false, title: Tea
Key: 3, selected: false, title: Coffee
Key: 4, selected: false, title: Water
Key: p_4, selected: true, title: Cereal
Key: 6, selected: true, title: Porridge
Key: p_5, selected: false, title: Dairy Product
and so on...

我正在使用fancytree在Laravel 5中发送此数据。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

$json = <<<JSON
{
"expanded":true,
"key":"root_1",
"selected":false,
"title":"root",
"children":
[
    {"key":"p_1","selected":false,"title":"Animal Meat"},
    {"key":"p_2","selected":false,"title":"Animal Product"},
    {"folder":true,"key":"p_3","selected":false,"title":"Beverage","children":  
    [
        {"key":"2","selected":false,"title":"Tea"},
        {"key":"3","selected":false,"title":"Coffee"},
        {"key":"4","selected":false,"title":"Water"}
    ]},
    {"folder":true,"key":"p_4","selected":true,"title":"Cereal","children": 
    [
        {"key":"6","selected":true,"title":"Porridge"}
    ]},
    {"folder":true,"key":"p_5","selected":false,"title":"Dairy Product","children":
    [
        {"key":"7","selected":false,"title":"Cheese"},
        {"key":"8","selected":false,"title":"Curd"},
        {"key":"10","selected":false,"title":"Milk"}
    ]}
]}
JSON;

$data = json_decode($json, true);

foreach ($data['children'] as $child) {
    $key      = $child['key'];
    $selected = $child['selected'];
    $title    = $child['title'];

    printf("Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
}

这将输出:

Key: p_1, selected: false, title: Animal Meat
Key: p_2, selected: false, title: Animal Product
Key: p_3, selected: false, title: Beverage
Key: p_4, selected: true, title: Cereal
Key: p_5, selected: false, title: Dairy Product

更新:我在我的示例中修改了JSON,因为在撰写本文时,原始帖子中的JSON无效(例如:包含原始JSON中的倒数第二行)一个尾随的逗号)。

Laravel方法:

public function storePreference(Request $request)
{
    $dataString = $request->getContent();
    $dataString = urldecode($data);
    $dataString = json_decode($dataString, 'data=');

    $data = json_decode($dataString, true);

    foreach($data['children'] as $child)
    {
       $key      = $child['key'];
        $selected = $child['selected'];
        $title    = $child['title'];

        printf("Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
    }
}

答案 1 :(得分:0)

我需要删除&#34; data =&#34;来自$ data string并使用json_decode。

public function storePreference(Request $request)
    {
        $data = $request->getContent('data');

        $data = urldecode($data);
        $data = substr($data, 5);
        $data = json_decode($data, true);
        foreach($data['children'] as $child)
        {
            $key      = $child['key'];
            $selected = $child['selected'];
            $title    = $child['title'];

            printf("<p>Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);

            if (isset($child['children'])) 
            {
                foreach ($child['children'] as $c) 
                {
                    $key      = $c['key'];
                    $selected = $c['selected'];
                    $title    = $c['title'];
                    printf("<p>Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);

                }
            }

        }
    }

感谢@jeromegamez的帮助。