doudao8283 2013-08-28 12:45
浏览 287
已采纳

json_decode返回空白

I am working on facebook app. I have stored some values in database in json format.

When I am fetching the value on the target page and trying to display the value as follow :

$sql = mysql_query("select * from `beforepublish` where `tabid` = '$page_id'") or die(mysql_error());
$sql = mysql_fetch_assoc($sql);

$contest_id = $sql['contestid'];
$temp_id = $sql["tempid"];
$value = $sql["values"];
$returnValue = json_decode($value);
echo "<pre>";
print_r($returnValue);
echo "</pre>";

Here the value of

$value = {"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab "}

and I am getting $returnValue as blank.

but When I try :

$returnValue = json_decode('{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab "}');
echo "<pre>";
print_r($returnValue);
echo "</pre>";

then $returnValue is returning :

stdClass Object
(
    [image] => upload_1182341221.jpg
    [conttxt] => Get a chance to win Samsung Galaxy Tab 
)

So guys, please help me, Why I am getting a blank value in the previous case ?

Edit

vardump($value) is returning :

string '{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab
    "}' (length=86)
  • 写回答

3条回答 默认 最新

  • doucai1901 2013-08-28 12:59
    关注
    string '{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab
        "}' (length=86)
    

    There is your problem. That linebreak and tab. It should look like:

    string '{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab"}' (length=84)
    

    This is why var_dump() outputs the length, so you can validate against any unwanted/invisible characters.


    You can store line breaks in javascript strings as .
    json_encode() will do this automatically.

    This applies for all escape sequences (commonly used ones are , , \t and \b).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况