Get value json

0

The server contains a .txt file ( link ) with the following information:

[
  {
    "Nome": "Hemerson",
    "data": "05/11",

  },
  {
    "Nome": "Hemerson",
    "data": "06/11",

  }
]

I want to get the information of the last name and date I would like to display on a page like this:

link

This way I can send it to my database.

    
asked by anonymous 04.09.2016 / 08:06

1 answer

0

The problem is that your json saved in the text document has an incorrect syntax ie it is not a json valid.

I do not know how you save in text but after each object and before closing them you put a comma, it is wrong so it invalidates your document as json if you can correct it, a functional example for whatever it would be something like this:

file.txt

 [ { "Nome": "Hemerson", "data": "05/11"

 }, { "Nome": "Hemerson", "data": "06/11"

 } ]

test.php

<?php
    // leia o arquivo
    $file = file_get_contents('file.txt');    

    // decodifique em array associativo para poder pegar a ultima posição
    $decode = json_decode($file, true);

    // pegue a ultima posição
    $ultimo = end($decode);

    // acesse o valor pela chave correspondente exemplo "data"
    echo $ultimo['data'];

    // isso ira imprimir: 06/11
    
04.09.2016 / 09:47