Una volta che hai i dati di PHP, è possibile utilizzare la funzione json_encode
; è fornito in bundle con PHP a partire da PHP 5.2
Nel tuo caso si stringa JSON rappresenta:
- una lista contenente 2 elementi
- ciascuno essere un oggetto, contenente 2 immobili/Valori
In PHP, ciò creerebbe la struttura che si sta rappresentando:
$data = array(
(object)array(
'oV' => 'myfirstvalue',
'oT' => 'myfirsttext',
),
(object)array(
'oV' => 'mysecondvalue',
'oT' => 'mysecondtext',
),
);
var_dump($data);
Il var_dump
si ottiene:
array
0 =>
object(stdClass)[1]
public 'oV' => string 'myfirstvalue' (length=12)
public 'oT' => string 'myfirsttext' (length=11)
1 =>
object(stdClass)[2]
public 'oV' => string 'mysecondvalue' (length=13)
public 'oT' => string 'mysecondtext' (length=12)
E, la codifica a JSON:
$json = json_encode($data);
echo $json;
Si ottiene:
[{"oV":"myfirstvalue","oT":"myfirsttext"},{"oV":"mysecondvalue","oT":"mysecondtext"}]
BTW: Frolm quello che mi ricordo, direi la tua stringa JSON non è valida-dati JSON: dovrebbero esserci virgolette attorno alla stringa, compresi i nomi delle proprietà degli oggetti
Vedere http://www.json.org/ per la grammatica.
Spero che questo aiuti :-)
fonte
2009-09-16 19:49:05
Questo ha funzionato come un incantesimo grazie. Anche se ho capito bene avrei dovuto usare "json_encode" sarebbe stato meglio programmare? – Haluk
Perché non usare semplicemente json_encode? –
@TimWachter Questa risposta è del 2009; nota il bit alla fine su "se hai una versione recente"; un discreto numero di host stava ancora eseguendo solo PHP 4 (e 'json_encode' è stato introdotto in 5.2). – Amber