1

I'm trying to pass a JSON string containing list of objects and decode it in php using json_decode(), I've tried many posts on StackOverflow but nothing worked. Here's what I'm trying to do:

JSON

"[{"description":"null","endDate":"2017-04-17","endTime":"15:00:00","eventID":"vmkqad951217ifc2a1j5ic9154","eventTitle":"Some Task","startDate":"2017-04-17","startTime":"14:00:00"},{"description":"here\u0027s the description","endDate":"2017-04-20","endTime":"19:30:00","eventID":"eq5ddd8rbhctf8gfcjh7t9r3o8","eventTitle":"Another task","startDate":"2017-04-20","startTime":"18:30:00"},{"description":"null","endDate":"2017-04-21","endTime":"16:00:00","eventID":"6dnrh44jspqlslqpdnujpk9btg","eventTitle":"Twother task","startDate":"2017-04-21","startTime":"15:00:00"}]"

PHP

$d = $_POST['data'];
$ar = json_decode($d,true);
$obj = json_decode($d);
var_dump($ar,$obj);

if I echo $d; it shows perfect result but var_dump($ar, $obj); both are NULL

I've also read this so its not the same. json_decode() returns null issues

Community
  • 1
  • 1
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25
  • 1
    If you actually have `"` at the front and back, use `$d = trim($d,'"');` to strip those before decoding. See if that works – Rasclatt Apr 16 '17 at 16:25
  • That's what [json_last_error](http://www.php.net/json_last_error) is for.. What does it say? – ccKep Apr 16 '17 at 16:32
  • [Working fine here](https://3v4l.org/TW4hN), so I suspect you're having those extra quotes at the beginning + end actually in your string, causing the decode to fail. – ccKep Apr 16 '17 at 16:36
  • @Rasclatt you are right........ its working now – AwaisMajeed Apr 16 '17 at 16:38

0 Answers0