这个正确的json格式?

时间:2013-05-17 16:54:01

标签: php javascript json

{
    "categories": [
        {
            "necklaces": [
                { title: "Outlet", url: "W_Outlet" },
                { title: "Baby-G", url: "W_BabyG" },
                { title: "Bulova", url: "W_Bulova" },
                { title: "Children_s", url: "W_Childrens" }
            ]
        }
    ]
}

当我在PHP中使用json_decode时,我得到一个空结果,这让我相信JSON格式不正确。

我需要的是:一个名为“categories”的对象,在该对象内部将有一个对象数组,其中包含一组对象。

即。类别 - > [项链 - > {title:'Gold',url:'Gold'}]

等等,但它似乎没有起作用。有什么想法吗?

4 个答案:

答案 0 :(得分:1)

{
    "categories": [
        {
            "necklaces": [
                {
                    "title": "Outlet",
                    "url": "W_Outlet"
                },
                {
                    "title": "Baby-G",
                    "url": "W_BabyG"
                },
                {
                    "title": "Bulova",
                    "url": "W_Bulova"
                },
                {
                    "title": "Children_s",
                    "url": "W_Childrens"
                }
            ]
        }
    ]
}

答案 1 :(得分:1)

您的字段名称周围缺少引号。

{
    "categories": [
        {
            "necklaces": [
                { "title": "Outlet", "url": "W_Outlet" },
                { "title": "Baby-G", "url": "W_BabyG" },
                { "title": "Bulova", "url": "W_Bulova" },
                { "title": "Children_s", "url": "W_Childrens" }
            ]
        }
    ]
}

答案 2 :(得分:1)

必须引用titleurl

<?php

$json = '{
    "categories": [
        {
            "necklaces": [
                { "title": "Outlet", "url": "W_Outlet" },
                { "title": "Baby-G", "url": "W_BabyG" },
                { "title": "Bulova", "url": "W_Bulova" },
                { "title": "Children_s", "url": "W_Childrens" }
            ]
        }
    ]
}';

$b = json_decode($json);
print_r($b);

?>

结果将是:

stdClass Object
(
    [categories] => Array
        (
            [0] => stdClass Object
                (
                    [necklaces] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [title] => Outlet
                                    [url] => W_Outlet
                                )

                            [1] => stdClass Object
                                (
                                    [title] => Baby-G
                                    [url] => W_BabyG
                                )

                            [2] => stdClass Object
                                (
                                    [title] => Bulova
                                    [url] => W_Bulova
                                )

                            [3] => stdClass Object
                                (
                                    [title] => Children_s
                                    [url] => W_Childrens
                                )

                        )

                )

        )

)

答案 3 :(得分:1)

所有键和值必须是JSON中的字符串,您缺少引号。

{
    "categories": [
        {
            "necklaces": [
                {
                    "title": "Outlet",
                    "url": "W_Outlet"
                },
                {
                    "title": "Baby-G",
                    "url": "W_BabyG"
                },
                {
                    "title": "Bulova",
                    "url": "W_Bulova"
                },
                {
                    "title": "Children_s",
                    "url": "W_Childrens"
                }
            ]
        }
    ]
}

您可以尝试JSONLint来测试JSON。