How to get all the data from the same .json file in PHP?

1

I have the following .json file

   {
    "Filmes": [
        {
            "Uuid": "",
            "ImdbId": "tt0023331",
            "Title": "Pesn o geroyakh",
            "Year": "1983",
            "Director": "Joris Ivens",
            "Actors": " Joris Ivens,  Joris Ivens",
            "Url": {
                "Poster": "https:\/\/www.imdb.com\/title\/tt0023331\/mediaviewer\/rm3586657792?ref_=tt_ov_i",
                "Location": ""
            }
        }
    ]
}
    {
    "Filmes": [
        {
            "Uuid": "aa-bb-cc-dd",
            "ImdbId": "tt0015724",
            "Title": "Dama de noche",
            "Year": "1993",
            "Director": "Eva López Sánchez",
            "Actors": "Rafael Sánchez Navarro, Cecilia Toussaint, Miguel Córcega",
            "Url": {
                "Poster": "https:\/\/www.imdb.com\/title\/tt0015724\/mediaviewer\/rm615620352?ref_=tt_ov_i",
                "Location": ""
            }
        }
    ]
}

And this is the code that the JSON file generates:

// 4. Llamada a la funcion read_id()
                $res = $filmes->read_id($filmes->id);

                // 5. Obtiene la cantidad de registros almacenados
                $num =$res->rowCount();

                // 6. Check si hay registros
                if ($num > 0)
                {
                    // 7. Vector-Objeto Filmes
                    $filmes_arr = array();
                    $filmes_arr['Filmes'] = array(); 

                    while ($row = $res->fetch(PDO::FETCH_ASSOC))
                    {
                       extract($row);
                       $filmes_item = array(
                            'Uuid' => $uuid_atto,
                            'ImdbId' =>  $tconst,
                            'Title'=> $primaryTitle,
                            'Year'=> $startYear,
                            'Director' => $director,
                            'Actors' => $actors,
                            'Url' => array(
                            'Poster' => $urlPoster,
                            'Location'=> $urlMovie)
                            );


                        // 8. Volcar los datos de filmes_item a "data"
                        array_push($filmes_arr['Filmes'], $filmes_item);
                    }

                    // 9. Convertir a JSON para imprimir
                    echo json_encode($filmes_arr, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
                    file_put_contents($ruta, json_encode($filmes_arr, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE), FILE_APPEND);
                } else {
                    // Sin registros
                    file_put_contents($ruta, json_encode(array('Message' => null), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
                }

But when I read with the PHP script:

$data = file_get_contents("/var/www/html/Json/filmes_id.json");
$imdb = json_decode($data, true);

foreach ($imdb as $imdbs)
{
     $id = ($imdbs[0]['ImdbId']);
     print_r($imdbs);
}

Only the ImdbId reads: tt0023331. For the ImdbId: tt0015724 and I return null.

I realized that only the first JSON object performs the reading.

    
asked by Bruno Soto 23.10.2018 в 13:02
source

2 answers

2

The point is that you are presenting a somewhat strange JSON.

I have tried to validate it in jsonlint and it does not validate.

What would most resemble a valid JSON would be this:

[{
    "Filmes": [{
        "Uuid": "",
        "ImdbId": "tt0023...",
        "Title": "Pens"
    }]
}, {
    "Filmes": [{
        "Uuid": "",
        "ImdbId": "tt0015...",
        "Title": "Dama de Noche"


    }]
}]

It would actually be an array, and inside it other objects whose key is Filmes and inside that key another array.

To read it in PHP the code would be like that.

/*Cadena de prueba*/
$str=
'
[{
    "Filmes": [{
        "Uuid": "",
        "ImdbId": "tt0023...",
        "Title": "Pens"
    }]
}, {
    "Filmes": [{
        "Uuid": "",
        "ImdbId": "tt0015...",
        "Title": "Dama de Noche"    
    }]
}]'; 

/*Código para leer*/
$json=json_decode($str);  //No pasamos TRUE, es preferible dejarlo como JSON
foreach ($json as $item){
    foreach ($item->Filmes as $filme){ //Array que hay en cada Filmes
        echo "ImdbId: $filme->ImdbId / Título: $filme->Title".PHP_EOL;
    }
}

Exit:

ImdbId: tt0023... / Título: Pens
ImdbId: tt0015... / Título: Dama de Noche

NOTICE that I have not forced the conversion of the object to an array by passing TRUE in json_decode . I have left it as a JSON object, because it is what it is. Then, the properties are accessed by the notation $objeto->propiedad , is even more elegant than $objeto["propiedad"] .

With a better structured JSON

I do not know if it is up to you to modify the JSON that is occurring. If it's up to you, maybe it's convenient to organize it in another way ...

If the JSON is better organized, it would be easier to read. For example:

/*Ejemplo de JSON mejor estructurado*/
$str=
    '
    {
    "Filmes":[
        {
            "Uuid": "",
            "ImdbId": "tt0023331",
            "Title": "Pesn o geroyakh",
            "Year": "1983",
            "Director": "Joris Ivens",
            "Actors": "Joris Ivens, Joris Ivens",
            "Url": {
                "Poster": "https:\/\/www.imdb.com\/title\/tt0023331\/mediaviewer\/rm3586657792?ref_=tt_ov_i",
                "Location": ""
            }
        },
        {
            "Uuid": "aa-bb-cc-dd",
            "ImdbId": "tt0015724",
            "Title": "Dama de noche",
            "Year": "1993",
            "Director": "Eva López Sánchez",
            "Actors": "Rafael Sánchez Navarro, Cecilia Toussaint, Miguel Córcega",
            "Url": {
                "Poster": "https:\/\/www.imdb.com\/title\/tt0015724\/mediaviewer\/rm615620352?ref_=tt_ov_i",
                "Location": ""
            }
        }
    ]
}
    ';

/*La lectura es más simple*/
$json=json_decode($str);             //No hace falta forzar a array
foreach ($json->Filmes as $filme){   //Accedemos directamente a la propiedad Filmes
    echo "ImdbId: $filme->ImdbId / Título: $filme->Title".PHP_EOL;
}

Exit:

ImdbId: tt0023331 / Título: Pesn o geroyakh
ImdbId: tt0015724 / Título: Dama de noche

Here, too, we do not force the array conversion, and we can access each film by simply doing this: $json->Filmes .

    
answered by 23.10.2018 в 13:42
1

I think the structure of the JSon is not correct, since you only have to declare the films once, and each film goes between {}, that's the right json:

 {
    "Filmes":[
        {
            "Uuid": "",
            "ImdbId": "tt0023331",
            "Title": "Pesn o geroyakh",
            "Year": "1983",
            "Director": "Joris Ivens",
            "Actors": "Joris Ivens, Joris Ivens",
            "Url": {
                "Poster": "https:\/\/www.imdb.com\/title\/tt0023331\/mediaviewer\/rm3586657792?ref_=tt_ov_i",
                "Location": ""
            }
        },
        {
            "Uuid": "aa-bb-cc-dd",
            "ImdbId": "tt0015724",
            "Title": "Dama de noche",
            "Year": "1993",
            "Director": "Eva López Sánchez",
            "Actors": "Rafael Sánchez Navarro, Cecilia Toussaint, Miguel Córcega",
            "Url": {
                "Poster": "https:\/\/www.imdb.com\/title\/tt0015724\/mediaviewer\/rm615620352?ref_=tt_ov_i",
                "Location": ""
            }
        }
    ]
}
    
answered by 23.10.2018 в 13:26