2014-02-24 22 views
0

你好,我已經JSON格式以下字符串..JSON是沒有轉換爲數組使用PHP

<?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字符串,我使用了下面的代碼..

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

但沒有任何工作。請幫忙解決這個謝謝

+1

好了,您發佈的字符串不是有效的JSON。在開始處刪除'dataSource:',}};'在最後,將所有的鍵轉換爲字符串(例如'id'應該是'「id」')。查看http://json.org/以瞭解JSON語法。 –

+0

來驗證你有一個有效的JSON,你可以嘗試[這裏](http://jsonlint.com/) –

+0

但這個字符串工作正常..如果它錯了,那麼什麼是正確的字符串 – Sachin

回答

1

它不會工作,因爲這不是一個有效的JSON。嘗試使用JS Linter。有許多可用的,甚至是基於網絡的e.g http://jsonformatter.curiousconcept.comhttp://www.jslint.com/

試試這個:

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

謝謝你的工作 – Sachin