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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改