PHP - Décodage JSON
J'ai le script suivant pour obtenir des résultats de recherche à partir d'une API, puis couper le tableau et le dump, j'ai de la difficulté du décodage JSON dans un tableau, il retourne Array(0) {
}
C'est un shortcode wordpress
Voici un échantillon du Json qui est obtenu à partir de l'api:
[
{
"barcode": "000015426950",
"name": "Karen's cowboy",
"author": "Ann",
"author_last": "Martin",
"publisher": "New York : Scholastic, c2000.",
"year_pub": "",
"edition": "",
"genre": "",
"checkout": "out",
"series": "",
"callnum": "MAR",
"fiction": "true",
"in_house": "false",
"timestamp": "1355835387",
"outto": "000008388615",
"duedate": "1372005722",
"ISBN": "059052528X",
"media_type": "",
"print": "false",
"BOXID": "2147483647",
"uid": "10",
"printed": ""
},
{
"barcode": "000015426949",
"name": "Karen's yo-yo",
"author": "Ann M",
"author_last": "Martin",
"publisher": "New York : Scholastic, c2000.",
"year_pub": "",
"edition": "",
"genre": "",
"checkout": "out",
"series": "",
"callnum": "MAR",
"fiction": "true",
"in_house": "false",
"timestamp": "1355835343",
"outto": "000008388615",
"duedate": "1373216918",
"ISBN": "0590525115",
"media_type": "",
"print": "false",
"BOXID": "",
"uid": "10",
"printed": ""
},
...
}
]
Voici le code utilisé pour obtenir le JSON et paginer,:
function book_search_form() {
?>
<form method='get'><input type='text' name='searchvalue' value='<? if (isset($_GET['searchvalue'])) echo $_GET['searchvalue'];?>' /> <input type='submit' value='Search' /><input type='hidden' name='pagenum' value='1' /></form>
<br>
<?php
if(isset($_GET['pagenum']) && isset($_GET['searchvalue']))
{
$page=$_GET['pagenum'];
$searchvalue = $_GET['searchvalue']; //get the value of the page from your url
$recordsPerPage=10; //number of records you want on your page
$api_url = get_option('api_url');
$api_key = get_option('api_key');
$data = file_get_contents("$api_url/book/index.php/name/$searchvalue?key=2513619352");
$array = (array)json_decode($data);
$index=($page*$recordsPerPage)-1;
$recordsToBeDisplayed = array_slice($array,$index,$recordsPerPage);//this array contains all the records you would want to display on a page;
$total_pages=ceil(count($array)/$recordsPerPage);
}
else {
//use default values
}
?>
<html>...<body><div id="records">
<?
echo '<pre>';
echo $recordsToBeDisplayed;
echo '</pre>';?><!-- use the array created above to display records -->
</div>
<div id="pagination">
<?
for($j=1;$j<=$total_pages;$j++){
if($j==$page)
{?>
<li style="display: inline;"><a href="?searchvalue=&pagenum=<?=$j?>"><u><?=$j?></u></a></li>
<?}else{?>
<li style="display: inline;"><a href="?searchvalue=&pagenum=<?=$j?>"><?=$j?></a></li>
<?}}?>
</div>
<?php
}
- double possible de Parsing JSON fichier avec PHP
Vous devez vous connecter pour publier un commentaire.
Essayer la fonction json_decode
Ensuite, vous aurez un tableau au lieu d'un objet.
Exemple #1 exemple avec json_decode ()
L'exemple ci-dessus va afficher:
Séparer le php du html s'il vous plaît, utilisez l'indentation et:
P. S: à Partir de la Bible (http://php.net/manual/en/function.json-decode.php)
Si l'entrée est en forme de tableau, puis aussi vous pouvez utiliser la même fonction json_decode. Vous avez juste besoin d'appliquer de la boucle pour le résultat.