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

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?