json没有使用php转换为数组

json没有使用php转换为数组,php,arrays,json,Php,Arrays,Json,您好,我有json格式的以下字符串 <?php echo $textjson='dataSource: [{ id: 1, text: "My Documents", expanded: true, spriteCssClass: "rootfolder", items: [ { id: 2, text: "Kendo UI Project", expanded: true, spriteCssClass: "fold

您好,我有json格式的以下字符串

<?php echo $textjson='dataSource: [{
        id: 1, text: "My Documents", expanded: true, spriteCssClass: "rootfolder", items: [
            {
                id: 2, text: "Kendo UI Project", expanded: true, spriteCssClass: "folder", items: [
                    { id: 3, text: "about.html", spriteCssClass: "html" },
                    { id: 4, text: "index.html", spriteCssClass: "html" },
                    { id: 5, text: "logo.png", spriteCssClass: "image" }
                ]
            },
            {
                id: 6, text: "New Web Site", expanded: true, spriteCssClass: "folder", items: [
                    { id: 7, text: "mockup.jpg", spriteCssClass: "image" },
                    { id: 8, text: "Research.pdf", spriteCssClass: "pdf" },
                ]
            },
            {
                id: 9, text: "Reports", expanded: true, spriteCssClass: "folder", items: [
                    { id: 10, text: "February.pdf", spriteCssClass: "pdf" },
                    { id: 11, text: "March.pdf", spriteCssClass: "pdf" },
                    { id: 12, text: "April.pdf", spriteCssClass: "pdf" }
                ]
            }
        ]
    }]
}); ' ?>

它不起作用,因为它不是有效的JSON。尝试使用jslinter。有许多可用的,甚至是基于网络的,例如

试试这个:

<?php

$textjson = '{
"dataSource": [{
        "id": 1, "text": "My Documents", "expanded": "true", "spriteCssClass": "rootfolder", "items": [
            {
                "id": 2, "text": "Kendo UI Project", "expanded": true,"spriteCssClass": "folder", "items": [
                    { "id": 3, "text": "about.html", "spriteCssClass": "html" },
                    { "id": 4, "text": "index.html", "spriteCssClass": "html" },
                    { "id": 5, "text": "logo.png", "spriteCssClass": "image" }
                ]
            },
            {
                "id": 6, "text": "New Web Site", "expanded": true, "spriteCssClass": "folder", "items": [
                    { "id": 7, "text": "mockup.jpg", "spriteCssClass": "image" },
                    { "id": 8, "text": "Research.pdf", "spriteCssClass": "pdf" }
                ]
            },
            {
                "id": 9, "text": "Reports", "expanded": true, "spriteCssClass": "folder", "items": [
                    { "id": 10, "text": "February.pdf", "spriteCssClass": "pdf" },
                    { "id": 11, "text": "March.pdf", "spriteCssClass": "pdf" },
                    { "id": 12, "text": "April.pdf", "spriteCssClass": "pdf" }
                ]
            }
        ]
    }]
}';

$json9 = json_decode($textjson);
print_r($json9);
?>


嗯,您发布的字符串不是有效的JSON。删除
数据源:
开头和
})id
应为
“id”
)。查看以了解JSON语法。若要验证是否有有效的JSON,可以尝试,但该字符串工作正常。。如果它是错误的,那么正确的字符串是什么?在JSON格式中,所有对象键都必须用双引号括起来
<?php

$textjson = '{
"dataSource": [{
        "id": 1, "text": "My Documents", "expanded": "true", "spriteCssClass": "rootfolder", "items": [
            {
                "id": 2, "text": "Kendo UI Project", "expanded": true,"spriteCssClass": "folder", "items": [
                    { "id": 3, "text": "about.html", "spriteCssClass": "html" },
                    { "id": 4, "text": "index.html", "spriteCssClass": "html" },
                    { "id": 5, "text": "logo.png", "spriteCssClass": "image" }
                ]
            },
            {
                "id": 6, "text": "New Web Site", "expanded": true, "spriteCssClass": "folder", "items": [
                    { "id": 7, "text": "mockup.jpg", "spriteCssClass": "image" },
                    { "id": 8, "text": "Research.pdf", "spriteCssClass": "pdf" }
                ]
            },
            {
                "id": 9, "text": "Reports", "expanded": true, "spriteCssClass": "folder", "items": [
                    { "id": 10, "text": "February.pdf", "spriteCssClass": "pdf" },
                    { "id": 11, "text": "March.pdf", "spriteCssClass": "pdf" },
                    { "id": 12, "text": "April.pdf", "spriteCssClass": "pdf" }
                ]
            }
        ]
    }]
}';

$json9 = json_decode($textjson);
print_r($json9);
?>