duanhe4155 2019-01-04 09:14
浏览 369

PHP:json_decode无法读取转义的字符串

i want to read a .json file using file_get_contents which returns a string. Then i use json_decode to create an array/object of it. Everything works.

My problem is that sometimes the string in the json file includes regex expressions. These can not be decoded! Therefore the following notice message:

Notice: Trying to get property 'Data' of non-object in 

After a quick google search i found out that i have to double escape them. I tried to change it in the json file - it works.

From:
      "Block Proxy URL": "somesite.ab123regex(\d\d\d).top/fyli.php",
To:
      "Block Proxy URL": "somesite.ab123regex(\\d\\d\\d).top/fyli.php",

But i receive that file generated by an appliance so i can't change it every time manually.

So consequently i tried to automate it:

$archer_json_data = file_get_contents($filename);
str_replace("\\", "\\\\", $archer_json_data);
$archer_json_array = json_decode($archer_json_data)->Data;

which does not work. I also tried addslashes instead of str_replace, it does not work. why? According to the replies of my quick google search it works for them.

For debugging purposes i echo'ed archer_json_data before str_replace and after.. they are exactly the same. Why does my str_replace not work?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题