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 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看