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
1 Answer 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);
?>
answered Feb 24, 2014 at 8:18
LawrenceGS
7031 gold badge6 silver badges13 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-php
dataSource:at the beginning and});at the end, and convert all keys to strings (e.g.idshould be"id"). Take a look at json.org to learn the JSON syntax.->json.org, json.org/example