PHP的德code JSON [英] PHP- Decode JSON
本文介绍了PHP的德code JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下脚本获得一个API的搜索结果,然后切片阵列和转储,我有麻烦了JSON解码成一个阵列,它返回阵列(0){
}
这是一个词preSS短code
下面是从API得到了JSON的示例:
[
{
酒吧code:000015426950,
名:卡伦的牛仔,
作者:安安,
author_last:马丁,
出版人:纽约:学术,C2000。
year_pub:,
版本:,
类型: ,
结帐:走出去,
系列:,
callnum:MAR,
虚构:真,
in_house:假,
时间戳:1355835387,
outto:000008388615,
交货期:1372005722,
ISBN:059052528X
媒体类型: ,
打印:假,
BOXID:2147483647,
UID:10,
印:
},
{
酒吧code:000015426949,
名:Karen的溜溜球,
作家:安M,
author_last:马丁,
出版人:纽约:学术,C2000。
year_pub:,
版本:,
类型: ,
结帐:走出去,
系列:,
callnum:MAR,
虚构:真,
in_house:假,
时间戳:1355835343,
outto:000008388615,
交货期:1373216918,
ISBN:0590525115,
媒体类型: ,
打印:假,
BOXID:,
UID:10,
印:
},
...
}
]
下面是code用于获取JSON和分页它:
函数book_search_form(){
?>
<形式方法='得到'><输入类型=文本名称='searchvalue'值='&LT ;?如果(使用isset($ _ GET ['searchvalue']))回声$ _GET ['searchvalue'];?>' />&安培; NBSP;<输入类型=提交值=搜索/><输入类型=隐藏的名字='页次'值=1/>< /表>
< BR>
< PHP
如果(使用isset($ _ GET ['页次'])及和放大器;使用isset($ _ GET ['searchvalue']))
{
$页= $ _ GET ['页次'];
$ searchvalue = $ _GET ['searchvalue']; //从您的网址获得页面的价值
$ recordsPerPage = 10; //你想让你的页面上的记录数
$ api_url = get_option('api_url');
$ API_KEY = get_option('API_KEY');
$数据=的file_get_contents($ api_url /电子书/ index.php文件/名/ $ searchvalue键= 2513619352?);
$阵列=(阵列)json_de code($的数据);$指数=($页* $ recordsPerPage)-1;
$ recordsToBeDisplayed = array_slice($数组$指数,$ recordsPerPage); //这个数组包含了所有你想在页面上显示的记录; $ TOTAL_PAGES = CEIL(计数($数组)/ $ recordsPerPage);
}
其他{//使用缺省值}
?>
< HTML和GT; ...<身体GT;< DIV ID =记载>
&LT ;?
呼应'< pre>';
回声$ recordsToBeDisplayed;
呼应'< / pre>';>< - !用上面创建显示记录的阵列 - >
< / DIV>
< DIV ID =分页>
&LT ;?
为($ J = 1; $ J< = $ TOTAL_PAGES; $ J ++){
如果(附加$ J $ ==页)
{&GT?; &LT;李风格=显示:内联;&GT;&LT;一href=\"?searchvalue=&pagenum=<?=$j?>\"><u><?=$j?></u></a></li>
&LT;}其他{&GT?;
&LT;李风格=???searchvalue =安培;页次=&LT; = $ J&gt;中?显示在线;&GT;&LT A HREF =&GT;&LT; = $ J&GT;&LT; / A&GT;&LT; /李&GT;
&LT;?}}&GT;
&LT; / DIV&GT;
&LT; PHP
}
解决方案
$阵列= json_de code($的数据,真实);
然后你会得到一个数组,而不是一个对象。
示例#1 json_de code()例子
&LT; PHP
$ JSON ='{一:1,B:2,C:3,D:4,e的:5}';后续代码var_dump(json_de code($ JSON));
后续代码var_dump(json_de code($ JSON,真实));?&GT;
上例将输出:
对象(stdClass的)#1(5){
[一] =&GT; INT(1)
[B] =&GT; INT(2)
[C] =&GT; INT(3)
[D] =&GT; INT(4)
[E] =&GT; INT(5)
}阵列(5){
[一] =&GT; INT(1)
[B] =&GT; INT(2)
[C] =&GT; INT(3)
[D] =&GT; INT(4)
[E] =&GT; INT(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的德code JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文