PHP-解码 JSON

Posted

技术标签:

【中文标题】PHP-解码 JSON【英文标题】:PHP- Decode JSON 【发布时间】:2013-11-05 17:21:53 【问题描述】:

我有以下脚本从 API 获取搜索结果,然后对数组进行切片并转储它,我无法将 JSON 解码为数组,它返回 Array(0) 这是一个wordpress简码

这是从 api 获取的 Json 示例:

[
  
    "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": ""
  ,
...
  
]

这是用于获取 JSON 并对其进行分页的代码:

function book_search_form() 
?>
<form method='get'><input type='text' name='searchvalue' value='<? if (isset($_GET['searchvalue'])) echo $_GET['searchvalue'];?>' />&nbsp;<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



【问题讨论】:

Parsing JSON file with PHP的可能重复 【参考方案1】:

如果输入是数组格式,那么你也可以使用相同的函数json_decode。您只需要为结果应用循环。

json = '

"a1": "field1":"name1", "field2":age1, "field3":"country1" ,

"a2": "field1":"name2", "field2":age2, "field3":"country2" ,

"a3": "field1":"name3", "field2":age3, "field3":"country3" ';

$Array = json_decode($json, true);

foreach ($Array as $key => $value)

    echo " $key ";

    foreach ($value as $k => $val)    
    
        echo "$k | $val <br />";
     

【讨论】:

【参考方案2】:

试试json_decode

$array = json_decode($data, true);

然后你会得到一个数组而不是一个对象。

示例 #1 json_decode() 示例

<?php
$json = '"a":1,"b":2,"c":3,"d":4,"e":5';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

上面的例子会输出:

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)

【讨论】:

【参考方案3】:

请把你的 php 和 html 分开,使用缩进和:

$array = json_decode($data, TRUE); //second param used for associative array return

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)

P.S:来自圣经 (http://php.net/manual/en/function.json-decode.php)

【讨论】:

以上是关于PHP-解码 JSON的主要内容,如果未能解决你的问题,请参考以下文章

Php json将所有值解码为字符串

来自localstorage的JSON无法在php中解码

如何使用 PHP 解码带有注释的 JSON 文件? [复制]

php PHP-JSON解码-error.php

kendoui parameterMap 解码 php 对象的正确 json 编码是啥?

如何在 PHP 5.1 中解码 json?