dongwuwei0718 2013-10-30 13:03
浏览 32
已采纳

使用php从字符串到数组的json

I have a problem with json parsing. I have already read the many questions here on stackoverflow but I can't figured out what I'm missing.

In my site I use Facebook Api to post my feed using curl and it respond with a json message. I take this response and I save it in my database.
In my Backoffice I need to retrieve this message and print it in case of error.

Here an example about an error message:

{"error":{"message":"(#1500) The url you supplied is invalid","type":"OAuthException","code":1500}}

In my php page I need to get just the message part so I did:

$message = get from the db and fetch;
$error_array = json_decode($message,true);
print_r($error_array);

but it doesn't print anything, just a blank page.
If I just print $message I can see the entire string.

What am I missing? This issue it's driving me crazy all day long!!

  • 写回答

2条回答 默认 最新

  • drevls8138 2013-10-30 13:07
    关注

    I tried the following:

    <pre>
    <?php
    
    $jsonStr = '{"error":{"message":"(#1500) The url you supplied is invalid","type":"OAuthException","code":1500}}';
    
    $error_array = json_decode($jsonStr, true);
    
    print_r($error_array);
    
    ?>
    

    and getting output:

    Array
    (
        [error] => Array
            (
                [message] => (#1500) The url you supplied is invalid
                [type] => OAuthException
                [code] => 1500
            )
    )
    

    It's working as intended.

    I suspect the problem is with this:

    $message = get from the db and fetch;

    After you load the $message variable, do a var_dump($message) and see if the string is in there (as expected).

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计