尝试从 php 编码 json 时遇到问题
Having Problems trying to encode json from php
所以,这是我的 php
代码:
$lista = Producoes::getPublicList();
if(count($lista) > 0){
$jsonObj = array();
foreach ($lista as $post){
$jsonPost = array();
$jsonPost["id"]=$post->id_post;
$jsonPost["url_post"]=$post->url_post;
$jsonPost["url_path"]=$post->url_path;
$jsonPost["url_foto"]=$post->url_foto;
$jsonPost["post_title"]=$post->post_title;
array_push($jsonObj, $jsonPost);
}
error_log( print_r($jsonObj,TRUE) );
}
Flight::json($jsonObj);
在错误日志中,我可以看到我得到了正确的输出结构:
Array(
[0] => Array
(
[id] => 1
[url_post] => http://www.blogbellafiore.com/2016/03/especial-de-pascoa-bella-fiore.html
[url_path] => especial-de-pascoa-bella-fiore.html
[url_foto] => https://4.bp.blogspot.com/-bd3jBX0GACc/Vt2qx4wfwuI/AAAAAAAAi-U/PvF_AtZhPOg/s640/GM_0003.jpg
[post_title] => Especial de Páscoa Bella Fiore
)
[1] => Array
(
[id] => 3
[url_post] => http://www.blogbellafiore.com/2013/03/editorial-de-pascoa.html
[url_path] => editorial-de-pascoa.html
[url_foto] => http://2.bp.blogspot.com/-ay94xBCYqtY/UUtHNsE2p1I/AAAAAAAAG1s/qjG6lS22IMg/s640/Pa%CC%81scoa-5.jpg
[post_title] => Editorial de Páscoa
)
[2] => Array
(
[id] => 2
[url_post] => http://www.blogbellafiore.com/2015/12/editorial-de-fotos-barraquinha-da-sophie.html
[url_path] => editorial-de-fotos-barraquinha-da-sophie.html
[url_foto] => https://2.bp.blogspot.com/-LF9rLCqpyl0/VnhNTfiLy2I/AAAAAAAAhlM/O-B1IwN_FJY/s640/Editorial_Barraquinha_da_Sophie__10.JPG
[post_title] => Editorial - Barraquinha da Sophie
)
)
但是 json 输出什么也没显示。只是空白。
我正在使用 FlightPHP 框架作为路由器,因此方法 Flight:json($var) 编码并 returns 一个 json 响应。已经在其他地方用过了,效果不错
@deceze 解决了,我只需要对 post 标题进行 utf8 编码,此修改有效:
$jsonPost["post_title"]=utf8_encode($post->post_title);
所以,这是我的 php
代码:
$lista = Producoes::getPublicList();
if(count($lista) > 0){
$jsonObj = array();
foreach ($lista as $post){
$jsonPost = array();
$jsonPost["id"]=$post->id_post;
$jsonPost["url_post"]=$post->url_post;
$jsonPost["url_path"]=$post->url_path;
$jsonPost["url_foto"]=$post->url_foto;
$jsonPost["post_title"]=$post->post_title;
array_push($jsonObj, $jsonPost);
}
error_log( print_r($jsonObj,TRUE) );
}
Flight::json($jsonObj);
在错误日志中,我可以看到我得到了正确的输出结构:
Array(
[0] => Array
(
[id] => 1
[url_post] => http://www.blogbellafiore.com/2016/03/especial-de-pascoa-bella-fiore.html
[url_path] => especial-de-pascoa-bella-fiore.html
[url_foto] => https://4.bp.blogspot.com/-bd3jBX0GACc/Vt2qx4wfwuI/AAAAAAAAi-U/PvF_AtZhPOg/s640/GM_0003.jpg
[post_title] => Especial de Páscoa Bella Fiore
)
[1] => Array
(
[id] => 3
[url_post] => http://www.blogbellafiore.com/2013/03/editorial-de-pascoa.html
[url_path] => editorial-de-pascoa.html
[url_foto] => http://2.bp.blogspot.com/-ay94xBCYqtY/UUtHNsE2p1I/AAAAAAAAG1s/qjG6lS22IMg/s640/Pa%CC%81scoa-5.jpg
[post_title] => Editorial de Páscoa
)
[2] => Array
(
[id] => 2
[url_post] => http://www.blogbellafiore.com/2015/12/editorial-de-fotos-barraquinha-da-sophie.html
[url_path] => editorial-de-fotos-barraquinha-da-sophie.html
[url_foto] => https://2.bp.blogspot.com/-LF9rLCqpyl0/VnhNTfiLy2I/AAAAAAAAhlM/O-B1IwN_FJY/s640/Editorial_Barraquinha_da_Sophie__10.JPG
[post_title] => Editorial - Barraquinha da Sophie
)
)
但是 json 输出什么也没显示。只是空白。
我正在使用 FlightPHP 框架作为路由器,因此方法 Flight:json($var) 编码并 returns 一个 json 响应。已经在其他地方用过了,效果不错
@deceze 解决了,我只需要对 post 标题进行 utf8 编码,此修改有效:
$jsonPost["post_title"]=utf8_encode($post->post_title);