-1

I have the following data

{
    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"
            }
    ]
}

When I try to var_dump it

It gives me null. How do I know whether the data is JSON encoded or not ?

Edit: Here's the code I get the above content by get_contents to the url

 $url = ""; // URL
 $contents = file_get_contents($url);
 $data = json_decode($contents);
 var_dump($data);
Javascript Coder
  • 5,691
  • 8
  • 52
  • 98
Narendra Rajput
  • 711
  • 9
  • 28
  • 2
    Can you show the code that gives `null`? – lonesomeday May 29 '12 at 12:40
  • 1
    Exactly what do you try to `var_dump`? – Leri May 29 '12 at 12:42
  • 3
    You can try to parse it with `json_decode` and test whether `json_last_error` returns any error code. – Felix Kling May 29 '12 at 12:42
  • 1
    possible duplicate of [How to determine whether a string is valid JSON?](http://stackoverflow.com/questions/1187576/how-to-determine-whether-a-string-is-valid-json) and [Detect bad json data in PHP json_decode()?](http://stackoverflow.com/questions/2348152/detect-bad-json-data-in-php-json-decode). – Felix Kling May 29 '12 at 12:43
  • BTW is it [proper format](http://www.json.org/index.html)? Names `total` and `list` without quotation marks. – Michał Powaga May 29 '12 at 12:44

2 Answers2

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

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • Can I access the properties of the Object normally by $str->list->title ? – Narendra Rajput May 29 '12 at 13:34
  • @NarendraRajput just apply json_decode to $str, like $str = json_decode($str); and there you go... also $str = json_decode($str, true); and you can access like this $str['list']['title'] ... – Dejan Marjanović May 29 '12 at 15:27
0

First whenever you will get any json value or you are sending any value in json format first check whether Json is valid or not..

Json validator and formator:-

http://jsonformatter.curiousconcept.com/

Json validator:-

http://www.jsonlint.org/

Then in Your code try to find error :- json_last_error

And try to use Json encode and Decode for Url values.. that will be helpful always..

Valid 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"}]}
Javascript Coder
  • 5,691
  • 8
  • 52
  • 98