0

I am making a web app. In one part of it, I have JS send a JSON string to PHP. The conent of the string is:

{"date":"24-03-2014","Cars":["Cheap","Expensive"]}

I want to convert the string into an object, for which I am doing:

$meta = $_POST["meta"];
$obj = json_decode($meta);
echo $obj->date;

Anyhow, Instead of having 24-03-2014 as the output, I am getting a blank line as the output.

What's wrong? What's the correct way of doing this?

coder
  • 1,809
  • 4
  • 19
  • 24

2 Answers2

0

Not able to re-produce it:

$jsonStr = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$jsonObj = json_decode($jsonStr);

var_dump($jsonObj);
var_dump($jsonObj->date);

Outputs:

object(stdClass)[1]
  public 'date' => string '24-03-2014' (length=10)
  public 'Cars' => 
    array (size=2)
      0 => string 'Cheap' (length=5)
      1 => string 'Expensive' (length=9)

string '24-03-2014' (length=10)

Are you sure your $_POST['meta'] is set & has values?

Latheesan
  • 23,247
  • 32
  • 107
  • 201
0

Below works like a charm. Your $_POST["date"] has not correct value inside. Try var_dump($_POST) to debug it.

<?php
  $input = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
  $meta = $input;
  $obj = json_decode($meta);
  var_dump($obj->date); //Prints string(10) "24-03-2014"
?>
makallio85
  • 1,366
  • 2
  • 14
  • 29