json没有使用php转换为数组

时间:2022-09-27 08:59:39

hello i have json formatted following string..

你好我有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" }
                ]
            }
        ]
    }]
}); ' ?>

i have tried lot of things to converting into an array but not getting any result. for converting my json string i used the following code..

我已经尝试了很多东西来转换成数组但没有得到任何结果。为了转换我的json字符串,我使用了以下代码..

<?php // echo 'hello' .$textjson; 
        //echo unserialize($textjson,true); 
        echo 'hellokjkvbh';
        echo $textjson;
        $json9=json_decode($textjson); 
        //print_r($textjson);
        print_r($json9); ?>

but nothing worked. please help to solve this thank you

但没有任何效果。请帮忙解决这个谢谢

1 个解决方案

#1


1  

It won't work because that is not a valid JSON. Try to use JS Linter. There are many available, even web-based e.g http://jsonformatter.curiousconcept.com , http://www.jslint.com/

它不起作用,因为它不是有效的JSON。尝试使用JS Linter。有很多可用的,甚至是基于网络的,例如http://jsonformatter.curiousconcept.com,http://www.jslint.com/

Try this:

尝试这个:

<?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);
?>

#1


1  

It won't work because that is not a valid JSON. Try to use JS Linter. There are many available, even web-based e.g http://jsonformatter.curiousconcept.com , http://www.jslint.com/

它不起作用,因为它不是有效的JSON。尝试使用JS Linter。有很多可用的,甚至是基于网络的,例如http://jsonformatter.curiousconcept.com,http://www.jslint.com/

Try this:

尝试这个:

<?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);
?>