Error When Listing Object Data in PHP

Asked

Viewed 109 times

1

How to list these data from Sponse, mainly IMG, follows the data BELOW:

  $data = file_get_contents("php://input");
  $objData = json_decode($data);

  $categoria = $objData->categoria->name_categoria;
  $titulo = $objData->titulo;
  $texto = $objData->texto;
  $data = $objData->data;
  $autor = $objData->autor;
  $status = $objData->status;
  $img = $objData->img->name;
{
   [functions]: ,
   __proto__: { },
   autor: "Luquinhas Brito",
   categoria: {
      [functions]: ,
      __proto__: { },
      date_categoria: "12/04/2017",
      id_categoria: 1,
      name_categoria: "Geral",
      status_categoria: "1",
      type_categoria: "news"
   },
   data: "26/04/2017",
   img: {
      [functions]: ,
      __proto__: { },
      lastModifiedDate: [date] Mon Jul 18 2016 20:53:17 GMT-0300 (Hora oficial do Brasil),
      name: "Capturar.JPG",
      size: 78623,
      type: "image/jpeg",
      webkitRelativePath: ""
   },
   status: "1",
   texto: "..",
   titulo: "Titulo Test"
}

Follow the error:

( ! ) Notice: Undefined property: stdClass::$name in C:\wamp64\www\Projeto_Sara\pages\admin\server-processing\cadastrarNews.php on line 12 Call Stack #TimeMemoryFunctionLocation 10.0005396592{main}( )...\index.php:0 20.0013432280router->routing( )...\index.php:69 30.03731824648require( 'C:\wamp64\www\Projeto_Sara\pages\admin\server-processing\cadastrarNews.php' )...\router.class.php:100  
  • 1

    json_decode on it! 11

  • No list, the img

  • $data = file_get_contents("php://input");
 $objData = json_decode($data);

 $categoria = $objData->categoria->name_categoria;
 $titulo = $objData->titulo;
 $texto = $objData->texto;
 $data = $objData->data;
 $autor = $objData->author; $status = $objData->status; $img = $objData->img->name;

  • Where did you get this data in json? And how do you send it to javascript?

  • A tip: Use the second parameter of the json_decode($json, true) function, as true so that the json_decode result is an array for easy data visualization.

1 answer

0

You do not have a valid json code, tested with jslint. You can test with the function echo json_last_error_msg (), it should return syntax error. Test like this:

$data = file_get_contents("php://input");
$objData = json_decode($data);

echo json_last_error_msg ();

$categoria = $objData->categoria->name_categoria;

Your json should have the format similar to this:

{
    "img": {
        "lastModifiedDate": "[date] Mon Jul 18 2016 20:53:17 GMT-0300 (Hora oficial do Brasil)",
        "name": "Capturar.JPG",
        "size": 78623,
        "type": "image/jpeg",
        "webkitRelativePath": ""
    }
}

You must fix the json encoding, before sending the content to your php.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.