1

So I have a json file inside my folder, but I cannot read it because it need an empty line at the end, how to solve that ?

I read my json in PHP using this :

$string = file_get_contents('ideb/NPWP.txt');
$json_a = json_decode($string, TRUE);
echo $json_a['header']['kodeReferensiPengguna'];

My json :

{
    "header": {
        "kodeReferensiPengguna": "",
        "tanggalHasil": "",
        "idPermintaan": "",
        "idPenggunaPermintaan": "",
        "dibuatOleh": "Dyta Alldilah",
        "kodeLJKPermintaan": "",
        "kodeCabangPermintaan": "",
        "kodeTujuanPermintaan": "",
        "tanggalPermintaan": "",
        "totalBagian": "1",
        "nomorBagian": "1"
    }
}

If I add newline at the bottom of the json there is no error

{
    "header": {
        "kodeReferensiPengguna": "",
        "tanggalHasil": "",
        "idPermintaan": "",
        "idPenggunaPermintaan": "",
        "dibuatOleh": "Dyta Alldilah",
        "kodeLJKPermintaan": "",
        "kodeCabangPermintaan": "",
        "kodeTujuanPermintaan": "",
        "tanggalPermintaan": "",
        "totalBagian": "1",
        "nomorBagian": "1"
    }
}


NOTE : My json file format is .txt

Maulana iman
  • 67
  • 1
  • 7

2 Answers2

0

The key kodeReferensiPengguna has no value. print dibuatOleh key there is value. it is working.

$string = file_get_contents('ideb/NPWP.txt');
$json_a = json_decode($string, TRUE);
echo $json_a['header']['dibuatOleh'];
  • ya it is working, turn out when I copy the json file from my document and copy it to my laravel folder somehow it is not working, but when i create the json file directly in my laravel folder it's working fine.. any solution ?? – Maulana iman Jan 29 '20 at 04:58
  • i think issue in path. are you putting correct path of file ? – Forge Web Design Jan 29 '20 at 05:02
  • nothing wrong with the path, my original json file which is from my document has no newline at the end of the text and I cannot read it, but when I create new file on laravel folder, copy paste the json text, it's working fine. smh... – Maulana iman Jan 29 '20 at 05:07
0

Solved thanks to @NikitaLeshchev..

Using :

$string = file_get_contents('ideb/NPWP.txt');
$json_a= mb_convert_encoding($string , 'UTF-8', 'UTF-8');
$json_a = json_decode($string, TRUE);
echo $json_a['header']['dibuatOleh'];
Maulana iman
  • 67
  • 1
  • 7