在 PHP 中解析时需要在 JSON 提要中保留反斜杠 [英] Need to keep backslashes in JSON feed when parsing in PHP

查看:75
本文介绍了在 PHP 中解析时需要在 JSON 提要中保留反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们的 JSON 提要之一的示例:

here is an example of one of our JSON feeds:

{"wiggins": {
  "id": "bkstir-04380-wdr-q",
  "alertcolour": "yellow",
  "infohvr": "",
  "infoclk": "",
  "warning": "10",
  "warnhvr": "There are 10 files stuck in \\server.domain.co.uk\Country\Dept\Output",
  "warnclk": "\\server.domain.co.uk\Country\Dept\Output",
  "process": "abc-app-015 Spooler",
  "processhvr": "",
  "processclk": "http://xyz-abc-001.svr.domain.co.uk/monitors/39.html"
}}

反斜杠阻止 php 显示它,如果我有一个没有任何反斜杠的 JSON,那么它们可以正常显示,但是我需要在那里使用斜杠,因为显示页面的人需要转到链接.为了让它现在工作,我也试过 stipslashes() 但它不起作用.任何人都可以提供任何建议吗?谢谢.这是解析文件的代码(正如我所说,只要没有反斜杠,这一点就可以工作):

the backslashes are stopping the php from displaying it, if I have a JSON without any backslashes in it then they display fine, however I need the slashes in there as the person displaying the page needs to go to the link. To get it working for now I've also tried stipslashes() but it doesn't work. Can anyone offer any advise? thanks. This is the code parsing the files (as I say, this bit works as long as there are no backslashes):

$path = "../../Admins/VBScript/Monitors/JSON/"; //where JSONs are stored
foreach (glob($path."*.json") as $file){ //loop through each file
$json = file_get_contents($file); //get JSON files
$data = json_decode($json, true); //parse the file

JSON 文件在我测试过的网站上有效.

The JSON files come back as valid on sites I've tested them on.

推荐答案

您的 JSON 无效.反斜杠必须像这样转义:

Your JSON is not valid. Backslashes have to be escaped like this:

{"wiggins": {
  "id": "bkstir-04380-wdr-q",
  "alertcolour": "yellow",
  "infohvr": "",
  "infoclk": "",
  "warning": "10",
  "warnhvr": "There are 10 files stuck in \\\\server.domain.co.uk\\Country\\Dept\\Output",
  "warnclk": "\\\\server.domain.co.uk\\Country\\Dept\\Output",
  "process": "abc-app-015 Spooler",
  "processhvr": "",
  "processclk": "http://xyz-abc-001.svr.domain.co.uk/monitors/39.html"
}}

根据这个问题你可以用

$json = str_replace('\\', '\\\\', $json);

修复您的 JSON.

这篇关于在 PHP 中解析时需要在 JSON 提要中保留反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆