2012-01-13 21 views
41

Ho la seguente JSON in un file list.txt:PHP leggere e scrivere JSON da file

{ 
"bgates":{"first":"Bill","last":"Gates"}, 
"sjobs":{"first":"Steve","last":"Jobs"} 
} 

Come faccio ad aggiungere "bross":{"first":"Bob","last":"Ross"} al mio file con PHP?

Ecco quello che ho finora:

<?php 

$user = "bross"; 
$first = "Bob"; 
$last = "Ross"; 

$file = "list.txt"; 

$json = json_decode(file_get_contents($file)); 

$json[$user] = array("first" => $first, "last" => $last); 

file_put_contents($file, json_encode($json)); 

?> 

che mi dà un errore fatale: Impossibile utilizzare oggetto di tipo stdClass come serie su questa linea:

$json[$user] = array("first" => $first, "last" => $last); 

sto usando PHP5. 2. qualche idea? Grazie!

risposta

77

L'indizio è nel messaggio di errore: se si osserva la documentazione di json_decode, si nota che può richiedere un secondo parametro, che controlla se restituisce un array o un oggetto - per impostazione predefinita è oggetto.

Quindi cambiare la vostra chiamata a

$json = json_decode(file_get_contents($file), true); 

Ed tornerà un array associativo e il codice dovrebbe funzionare bene.

+3

Detesto che 'json_decode' di default restituisca una classe anziché una matrice. Questo mi eccita ogni volta che uso 'json_decode' per la prima volta in un mese. – Tim

7

È necessario rendere la funzione di decodifica restituire un array passando il parametro true.

json_decode(file_get_contents($file),true);

3

Provare a utilizzare secondo parametro per la funzione json_decode:

$json = json_decode(file_get_contents($file), true); 
8

o semplicemente usare $ JSON come oggetto:

$json->$user = array("first" => $first, "last" => $last); 

questo è come viene restituito senza il secondo parametro (come esempio di stdClass).

3

Questo dovrebbe funzionare per voi per ottenere il contenuto di list.txt file di

$headers = array('http'=>array('method'=>'GET','header'=>'Content: type=application/json \r\n'.'$agent \r\n'.'$hash')); 

$context=stream_context_create($headers); 

$str = file_get_contents("list.txt",FILE_USE_INCLUDE_PATH,$context); 

$str=utf8_encode($str); 

$str=json_decode($str,true); 

print_r($str); 
+0

Ho appena ottenuto Array come output da quello –

18

Il campione per la lettura e la scrittura di JSON in PHP:

$json = json_decode(file_get_contents($file),TRUE); 

$json[$user] = array("first" => $first, "last" => $last); 

file_put_contents($file, json_encode($json)); 
+1

Il secondo argomento di json_encode non è un booleano, quindi è sbagliato scrivere 'json_encode ($ json, TRUE)'. –

+0

Per i documenti (http://php.net/manual/en/function.json-decode.php) il secondo argomento È un valore booleano. –

1

Quando si desidera creare formato JSON doveva essere in questo formato per leggere:

[ 
    { 
    "":"", 
    "":[ 
    { 
     "":"", 
     "":"" 
    } 
    ] 
    } 
] 
2

Se si desidera visualizzare bene i dati JSON definito formate è possibile modificare il codice come:

file_put_contents($file, json_encode($json,TRUE)); 


$headers = array('http'=>array('method'=>'GET','header'=>'Content: type=application/json \r\n'.'$agent \r\n'.'$hash')); 

$context=stream_context_create($headers); 

$str = file_get_contents("list.txt",FILE_USE_INCLUDE_PATH,$context); 

$str1=utf8_encode($str); 

$str1=json_decode($str1,true); 



foreach($str1 as $key=>$value) 
{ 

    echo "key is: $key.\n"; 

    echo "values are: \t"; 
     foreach ($value as $k) { 

     echo " $k. \t"; 
     # code... 
    } 
     echo "<br></br>"; 
     echo "\n"; 

} 
Problemi correlati