0

hello i have json formatted following string..

<?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..

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

8
  • 1
    Well, the string you posted is not valid JSON. Remove dataSource: at the beginning and }); at the end, and convert all keys to strings (e.g. id should be "id"). Take a look at json.org to learn the JSON syntax. Commented Feb 24, 2014 at 7:54
  • to verify that you have a valid JSON , you can try here Commented Feb 24, 2014 at 7:55
  • but this string is working properly .. and if its wrong then what will be the correct string Commented Feb 24, 2014 at 7:55
  • -> json.org, json.org/example Commented Feb 24, 2014 at 7:56
  • 1
    In JSON format all object keys must be wrapped by double-quotes Commented Feb 24, 2014 at 7:59

1 Answer 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/

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);
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.