Stránka: 1 z 1
| [ Príspevkov: 9 ] | |
| Autor | Správa |
|---|
Registrovaný: 07.03.08 Prihlásený: 19.12.09 Príspevky: 19 Témy: 7 | 7 | |
Registrovaný: 02.03.07 Prihlásený: 03.07.25 Príspevky: 70 Témy: 1 | 1 Bydlisko: Košice[JZR] |
V php existuje od verzie 5.2 metoda
Kód: mixed json_decode ( string $json [, bool $assoc= false [, int $depth= 512 ]] ) ktora vracia pole/objekt. Ak vsak mas verziu <5.2 tak pouzi so stiahnutou triedou Citácia: http://mike.teczno.com/JSON/JSON.phps Kód: <?php if ( !function_exists('json_decode') ){ function json_decode($content, $assoc=false){ require_once 'Services/JSON.php'; if ( $assoc ){ $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); } else { $json = new Services_JSON; } return $json->decode($content); } }
if ( !function_exists('json_encode') ){ function json_encode($content){ require_once 'Services/JSON.php'; $json = new Services_JSON; return $json->encode($content); } } ?>
|
|
Registrovaný: 07.03.08 Prihlásený: 19.12.09 Príspevky: 19 Témy: 7 | 7 |
Nejak mi to s tou triedou nefunguje.
Mohli by ste mi napísať hotový script - do premennej dám json a pekne mi to vypíše?
|
|
Registrovaný: 02.03.07 Prihlásený: 03.07.25 Príspevky: 70 Témy: 1 | 1 Bydlisko: Košice[JZR] |
Mne to vsetko v pohode ide takze napisem podrobnejsi postup:
Citácia: 1) Stiahnes si subor Kód: http://mike.teczno.com/JSON/JSON.phps a das ho niekde do priecinka s hlavnym suborom, pomenujes ho s priponou Kód: .php , nie Kód: .phps ! Citácia: 2) Niekde (bud v osobitnom subore s tvojimi funkciami, alebo rovno v 'hlavnom subore', tj. kde uz parsujes tie veci z twitteru si zadeklarujes tie funkcie json_encode a json_decode: Kód: if ( !function_exists('json_decode') ){ function json_decode($content, $assoc=false){ // TU SI ZMEN CESTU A SUBOR KDE SI ULOZIL require_once 'Services/JSON.php'; if ( $assoc ){ $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); } else { $json = new Services_JSON; } return $json->decode($content); } }
if ( !function_exists('json_encode') ){ function json_encode($content){ // TU SI ZMEN CESTU A SUBOR KDE SI ULOZIL require_once 'Services/JSON.php'; $json = new Services_JSON; return $json->encode($content); } } Citácia: 3) A teraz len volas json_decode: Kód: json_decode($premenna_s_json, true); a teda ked chces presne format nieco, nieco2 tak das Kód: echo(implode(', ', json_decode($premenna_s_json, true));
uz dufam ze chapete 
|
|
Registrovaný: 07.03.08 Prihlásený: 19.12.09 Príspevky: 19 Témy: 7 | 7 |
To s umiestnením ma napadlo, ale json_decode($premenna_s_json, true); som nevedel.
Vypísalo mi
1238169699, Array
a to je všetko - len prvý parameter
JSON bol
Kód: {"as_of":1238169699,"trends":{"2009-03-27 16:01:39":[{"query":"#gov20camp","name":"#gov20camp"},{"query":"\"Earth Hour\"","name":"Earth Hour"},{"query":"TGIF","name":"TGIF"},{"query":"#pycon","name":"#pycon"},{"query":"Fargo","name":"Fargo"},{"query":"Spring","name":"Spring"},{"query":"#FDC","name":"#FDC"},{"query":"Duke","name":"Duke"},{"query":"iPhone","name":"iPhone"},{"query":"Afghanistan","name":"Afghanistan"}]}}
|
|
Registrovaný: 02.03.07 Prihlásený: 03.07.25 Príspevky: 70 Témy: 1 | 1 Bydlisko: Košice[JZR] |
Teda, po par minutach rozmyslania a hladania som prisiel na:
Treba pridat este funkciu, ktora spoji vsetky elementy pola a zaroven aj uplne vsetky vnorene polia:
Kód: function array_implode($arrays, &$target) { foreach ($arrays as $item) { if (is_array($item)) { array_implode($item, $target); } else { $target .= $item . ', '; } } }
a vola sa takto (uz aj s tym jsonom): Kód: $jsonned = ''; array_implode(json_decode($json, true), $jsonned); echo $jsonned;
|
|
Registrovaný: 07.03.08 Prihlásený: 19.12.09 Príspevky: 19 Témy: 7 | 7 |
Tú ďalšiu funkciu som tam dal a vypísalo mi toto:
Warning: Invalid argument supplied for foreach() in /var/www/web5/*****.eu/*****.eu/jsontest.php on line 27
Celý zdroj:
Kód: <?php $t = '{"as_of":1238169699,"trends":{"2009-03-27 16:01:39":[{"query":"#gov20camp","name":"#gov20camp"},{"query":"\"Earth Hour\"","name":"Earth Hour"},{"query":"TGIF","name":"TGIF"},{"query":"#pycon","name":"#pycon"},{"query":"Fargo","name":"Fargo"},{"query":"Spring","name":"Spring"},{"query":"#FDC","name":"#FDC"},{"query":"Duke","name":"Duke"},{"query":"iPhone","name":"iPhone"},{"query":"Afghanistan","name":"Afghanistan"}]}}' ;
if ( !function_exists('json_decode') ){ function json_decode($content, $assoc=false){ require_once 'Services/JSON.php'; if ( $assoc ){ $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); } else { $json = new Services_JSON; } return $json->decode($content); } }
if ( !function_exists('json_encode') ){ function json_encode($content){ require_once 'Services/JSON.php'; $json = new Services_JSON; return $json->encode($content); } } function array_implode($arrays, &$target) { foreach ($arrays as $item) { if (is_array($item)) { array_implode($item, $target); } else { $target .= $item . ', '; } } }
$jsonned = $t ; array_implode(json_decode($json, true), $jsonned); echo $jsonned; ?>
|
|
Registrovaný: 02.03.07 Prihlásený: 03.07.25 Príspevky: 70 Témy: 1 | 1 Bydlisko: Košice[JZR] |
$json nahradit na $t:
Kód: array_implode(json_decode($t, true), $jsonned); Toto ma byt len inicializacia premennej, teda nepriradzovat jej nic: Kód: $jsonned = '';

|
|
Registrovaný: 07.03.08 Prihlásený: 19.12.09 Príspevky: 19 Témy: 7 | 7 |
Ďakujem veľmi pekne 
|
|
Stránka: 1 z 1
| [ Príspevkov: 9 ] | |
|