PHP-解码JSON [英] PHP- Decode JSON
问题描述
我有以下脚本从 API 获取搜索结果,然后对数组进行切片并转储,我无法将 JSON 解码为数组,它返回 Array(0) {}
这是一个 wordpress 简码
这里是从 api 获取的 Json 示例:
<预><代码>[{"条码": "000015426950","name": "卡伦的牛仔","作者": "安","author_last": "马丁","publisher": "纽约:Scholastic,c2000.","year_pub": "","版本": "",类型": "","结帐": "结帐",系列": "","callnum": "MAR",小说":真实","in_house": "假","时间戳": "1355835387","outto": "000008388615","截止日期": "1372005722","ISBN": "059052528X",媒体类型": "",打印":假","BOXID": "2147483647","uid": "10",打印": ""},{"条码": "000015426949","name": "凯伦的溜溜球","author": "Ann M","author_last": "马丁","publisher": "纽约:Scholastic,c2000.","year_pub": "","版本": "",类型": "","结帐": "结帐",系列": "","callnum": "MAR",小说":真实","in_house": "假","时间戳": "1355835343","outto": "000008388615","截止日期": "1373216918","ISBN": "0590525115",媒体类型": "",打印":假","BOXID": "","uid": "10",打印": ""},...}]这是用于获取 JSON 并对其进行分页的代码:
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><?phpif(isset($_GET['pagenum']) &&isset($_GET['searchvalue'])){$page=$_GET['pagenum'];$searchvalue = $_GET['searchvalue'];//从你的 url 中获取页面的值$recordsPerPage=10;//你想要在页面上的记录数$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);//这个数组包含你想在页面上显示的所有记录;$total_pages=ceil(count($array)/$recordsPerPage);}别的 {//使用默认值}?><html>...<body><div id="records"><?echo '';回声 $recordsToBeDisplayed;echo '</pre>';?><!-- 使用上面创建的数组来显示记录-->
<div id="分页"><?for($j=1;$j<=$total_pages;$j++){如果($j==$page){?><li style="display: inline;"><a href="?searchvalue=&pagenum=<?=$j?>"><u><?=$j?>;</u></a></li><?}其他{?><li style="display: inline;"><a href="?searchvalue=&pagenum=<?=$j?>"><?=$j?></></li><?}}?>
<?php}
尝试 json_decode
$array = json_decode($data, true);
然后你会得到一个数组而不是一个对象.
示例 #1 json_decode() 示例
上面的例子会输出:
object(stdClass)#1 (5) {["a"] =>整数(1)[b"] =>整数(2)[c"] =>整数(3)[d"] =>整数(4)[e"] =>整数(5)}数组(5){["a"] =>整数(1)[b"] =>整数(2)[c"] =>整数(3)[d"] =>整数(4)[e"] =>整数(5)}
I have the following script to get search results from an API and then slice the array and dump it, I am having trouble decoding the JSON into an array, it returns Array(0) {
}
It is a wordpress shortcode
Here is a sample of the Json that is gotten from the 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": ""
},
...
}
]
Here is the code used to get the JSON and paginate it,:
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
}
Try json_decode
$array = json_decode($data, true);
Then you'll get an array instead of an object.
Example #1 json_decode() examples
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
The above example will output:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
这篇关于PHP-解码JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!