2012-05-29 19 views
-1

我有以下數據認識JSON內容

{ 
    total: "156", 
    list: [ 

      { 
      "nodeRef": "workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67", 
      "id": "e364714d-14bc-4e13-bfff-c1f86a8cbe67", 
      "name": "Morning Class_Dadi Janki_29-05-12_H_London.mp4", 
      "mimetype": "video/mp4", 
      "title" : "Morning Class" , 
      "author": "Dadi Janki", 
      "class_date": "May 29, 2012 12:00:00 AM", 
      "created": "May 29, 2012 12:32:44 PM", 
      "size": "97,156,420", 
      "lang": "h", 
      "totalViews": "11", 
      "totalDownloads": "0", 
      "downloadUrl": "/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4" 
      } 
    ] 
} 

當我嘗試var_dump

它給了我零。我如何知道數據是否爲JSON編碼?

編輯: 下面的代碼 我通過get_contents得到上述內容到url

$url = ""; // URL 
$contents = file_get_contents($url); 
$data = json_decode($contents); 
var_dump($data); 
+2

你可以顯示給出'null'的代碼嗎? – lonesomeday

+1

準確地說你嘗試'var_dump'? – Leri

+3

你可以嘗試用'json_decode'解析它,並測試'json_last_error'是否返回任何錯誤代碼。 –

回答

1
<?php 

$str = '{ 
    total: "156", 
    list: [ 

      { 
      "nodeRef": "workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67", 
      "id": "e364714d-14bc-4e13-bfff-c1f86a8cbe67", 
      "name": "Morning Class_Dadi Janki_29-05-12_H_London.mp4", 
      "mimetype": "video/mp4", 
      "title" : "Morning Class" , 
      "author": "Dadi Janki", 
      "class_date": "May 29, 2012 12:00:00 AM", 
      "created": "May 29, 2012 12:32:44 PM", 
      "size": "97,156,420", 
      "lang": "h", 
      "totalViews": "11", 
      "totalDownloads": "0", 
      "downloadUrl": "/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4" 
      } 
    ] 
} 
'; 

$str = preg_replace('#([^\s\"]+): #is', '"\\1": ', $str); 

echo $str; 

?> 

enter image description here

+0

真棒!保存我的日子 –

+0

我可以通過$ str-> list-> title正常訪問Object的屬性嗎? –

+0

@NarendraRajput只需將json_decode應用於$ str,就像$ str = json_decode($ str);並且你去...也是$ str = json_decode($ str,true);你可以像這樣訪問$ str ['list'] ['title'] ... –

0

首先,只要你會得到任何JSON值或您發送的任何值在json格式中首先檢查Json是否有效..

JSON驗證和formator: -

http://jsonformatter.curiousconcept.com/ 

JSON驗證: -

http://www.jsonlint.org/ 

然後在你的代碼試圖找到錯誤: - json_last_error

並嘗試使用JSON編碼和解碼的網址值..這將是總是有幫助..

有效的JSON: -

{ 
    "total":"156", 
    "list":[ 
     { 
     "nodeRef":"workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67", 
     "id":"e364714d-14bc-4e13-bfff-c1f86a8cbe67", 
     "name":"Morning Class_Dadi Janki_29-05-12_H_London.mp4", 
     "mimetype":"video/mp4", 
     "title":"Morning Class", 
     "author":"Dadi Janki", 
     "class_date":"May 29, 2012 12:00:00 AM", 
     "created":"May 29, 2012 12:32:44 PM", 
     "size":"97,156,420", 
     "lang":"h", 
     "totalViews":"11", 
     "totalDownloads":"0", 
     "downloadUrl":"/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4" 
     } 
    ] 
} 


Json:-- 

{"total":"156","list":[{"nodeRef":"workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67","id":"e364714d-14bc-4e13-bfff-c1f86a8cbe67","name":"Morning Class_Dadi Janki_29-05-12_H_London.mp4","mimetype":"video/mp4","title":"Morning Class","author":"Dadi Janki","class_date":"May 29, 2012 12:00:00 AM","created":"May 29, 2012 12:32:44 PM","size":"97,156,420","lang":"h","totalViews":"11","totalDownloads":"0","downloadUrl":"/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"}]}