简体   繁体   中英

PHP JSON returning NULL

I've got a JSON file, which I want to retrieve with PHP, but when I try to show the values on screen, it returns NULL.

JSON:

{
"categorias": [
    {
        "nombre": "Fundamentos",
        "subcategorias": [
            {
                "nombre": "Colores",
                "modulos": [
                    {
                        "seccion": "<b>Colores</b>",
                        "objetos": [
                            {
                                "imagen": "sketches/fundamentos/preview/colores.png",
                                "titulo": "",
                                "texto": "",
                                "class": "full-width",
                                "noLink": true
                            }
                        ]
                    }
                ]
            },
            {
                "nombre": "Tipografías",
                "modulos": [
                    {
                        "seccion": "<b>Tipografías</b>",
                        "objetos": [
                            {
                                "imagen": "sketches/fundamentos/preview/tipografias.png",
                                "titulo": "",
                                "texto": "",
                                "class": "full-width",
                                "noLink": true
                            }
                        ]
                    }
                ]
            },
            {
                "nombre": "Iconografía",
                "modulos": [
                    {
                        "seccion": "<b>Iconografía</b>",
                        "objetos": [
                            {
                                "imagen": "sketches/fundamentos/preview/tipografias.png",
                                "titulo": "",
                                "texto": "",
                                "class": "full-width",
                                "noLink": true
                            }
                        ]
                    }
                ]
            }
        ],
        "unique": false
    }
]}

I read and tried many other solutions, that json must be encoded with utf-8, but I couldn't achieve it. It's really been a while since I coded with PHP, so I'm very rusty right now.

PHP:

$archivo = file_get_contents("../json_info.json");
$json_data = json_decode($archivo); //here I also tried to include "encode"
var_dump($json_data);

Also I tried:

$error = json_last_error();

With encode_json returns 0, and without it returns a 4.

I hope you can lend me a hand with it.

Edit/Update

As some told me, I tried adding the json in the same directory. Added also "true" to json_decode:

$archivo = file_get_contents("json_info.json");
$json_data = json_decode($archivo, true);

And this returns NULL.

Your json is very poorly built. But i don't know. Maybe you have to work with it. This code works 100%;

For example

$archivo = file_get_contents("../json_info.json");
$json_data = json_decode($archivo);

print_r($json->categorias[0]->nombre);

You get

Fundamentos

OR

  print_r($json->categorias[0]->subcategorias);

Outputs

Array ( [0] => stdClass Object ( [nombre] => Colores [modulos] => Array ( [0] => stdClass Object ( [seccion] => Colores [objetos] => Array ( [0] => stdClass Object ( [imagen] => sketches/fundamentos/preview/colores.png [titulo] => [texto] => [class] => full-width [noLink] => 1 ) ) ) ) ) [1] => stdClass Object ( [nombre] => Tipografías [modulos] => Array ( [0] => stdClass Object ( [seccion] => Tipografías [objetos] => Array ( [0] => stdClass Object ( [imagen] => sketches/fundamentos/preview/tipografias.png [titulo] => [texto] => [class] => full-width [noLink] => 1 ) ) ) ) ) [2] => stdClass Object ( [nombre] => Iconografía [modulos] => Array ( [0] => stdClass Object ( [seccion] => Iconografía [objetos] => Array ( [0] => stdClass Object ( [imagen] => sketches/fundamentos/preview/tipografias.png [titulo] => [texto] => [class] => full-width [noLink] => 1 ) ) ) ) ) ) 

It is right. You have 3 items in your top object categories. subcategorias has 3 dictionaries. Everything is nice. The only problem you could have is path. If you build this script in other, it will get current path from executable file and not from one that is included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM