NULL value when passing data json file to php

0

I have a json file with this element:

{ 
    "id":"1",
    "nombre":"Luis",
    "Descripción":"Alumno"
}

The php file looks like this:

<?php 
$json = file_get_contents("json.json");

$data = json_decode($json,true);
var_dump($data);
?>

Well, the var_dump tells me it's NULL. Any solution?

    
asked by Borja Sanchez 17.07.2017 в 15:53
source

1 answer

1

To mount the json the fields must be encoded in utf-8. An example:

$result = array();
$result['id'] = (int)1;
$result['nombre'] = utf8_encode("Luis");
$result['descripcion'] = utf8_encode("Alumno"); //mejor no uses tildes en el nombre del campo

header("Content-Type: application/json; charset=UTF-8");
echo json_encode($result);

And then you read it with your php file. json_decode requires the encoding to be utf-8.

$json = file_get_contents("json.php");
var_dump($json);

$data = json_decode($json,true);
var_dump($data);

Now it should work.

    
answered by 17.07.2017 / 17:32
source