我需要解码 json 的帮助
if($loop->have_posts()) :
$json = '{';
$json .= '
"api_status":1,
"api_message":"success",
"data": [';
while ( $loop->have_posts() ) : $loop->the_post();
$json .= '{
"id":'.get_the_ID().',
"post_name":"'.get_the_title().'"
},
';
endwhile;
$json = substr($json,0,-1);
$json .= ']}';
echo $json;
endif;
break;
}
我的错误是
在最后一个}
中我仍然有,
,所以我需要删除它。
但我不知道怎么办? 有人帮助我吗?
最佳答案
正如评论中提到的,json_encode
是正确的选择。
$toEncode = array(
"api_status" => 1,
"api_message" => "success",
"data" => array()
);
while ($loop->have_posts()) {
$loop->the_post();
array_push($toEncode["data"], array(
"id" => get_the_ID(),
"post_name" => get_the_title()
));
}
echo json_encode($toEncode);
但是,我不太明白你们的帖子系统是如何工作的。您是否使用某种类型的迭代器?
关于javascript - 解码json代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40173260/