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

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

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

ie Category -> [ Necklaces -> { title: 'Gold', url: 'Gold' } ]

等等,但它似乎没有工作。有任何想法吗?

4

4 回答 4

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"
                }
            ]
        }
    ]
}
于 2013-05-17T16:57:07.650 回答
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" }
            ]
        }
    ]
}
于 2013-05-17T16:57:25.430 回答
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
                                )

                        )

                )

        )

)
于 2013-05-17T16:59:37.387 回答
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。

于 2013-05-17T16:59:39.423 回答