【问题标题】:How do I json_decode string with special chars (" \\ " )如何使用特殊字符 (" \\ " ) 对字符串进行 json_decode
【发布时间】:2017-01-25 02:18:27
【问题描述】:

我在编码和解码 json 数据时遇到问题。在 js 中,我发送数据类型为 'json' 的查询,如下所示:

{\"front\":{\"0\":{\"type\":\"text\",\"width\":\"55px\",\"height\":\"27px\",\"top\":\"151px\",\"left\":\"86px\",\"zIndex\":\"1\",\"svg\":\"<svg width=\\\"54.9375\\\" height=\\\"27.09375\\\" viewBox=\\\"0 0 54.9375 27.09375\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" xmlns:xlink=\\\"http://www.w3.org/1999/xlink\\\"><g id=\\\"0.7882792934370437\\\"><text fill=\\\"#FF0000\\\" stroke=\\\"none\\\" stroke-width=\\\"0\\\" stroke-linecap=\\\"round\\\" stroke-linejoin=\\\"round\\\" x=\\\"\\\" y=\\\"\\\" text-anchor=\\\"start\\\" font-size=\\\"24px\\\" font-family=\\\"arial\\\" data-textcurve=\\\"1\\\" data-itemzoom=\\\"1 1\\\" data-textspacing=\\\"0\\\"><textPath xmlns:xlink=\\\"http://www.w3.org/1999/xlink\\\" xlink:href=\\\"http://138.68.62.219/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\"><tspan dy=\\\"0\\\">Hello</tspan></textPath></text></g><defs><path id=\\\"textPath-item-0\\\" d=\\\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\"></path></defs></svg>\",\"rotate\":0,\"text\":\"Hello\",\"color\":\"#FF0000\",\"fontFamily\":\"arial\",\"align\":\"center\",\"outlineC\":\"none\",\"outlineW\":0}}

然后我将它保存到MySQL的单元格中,它看起来像这样:

{\\&quot;front\\&quot;:{\\&quot;0\\&quot;:{\\&quot;type\\&quot;:\\&quot;text\\&quot;,\\&quot;width\\&quot;:\\&quot;55px\\&quot;,\\&quot;height\\&quot;:\\&quot;27px\\&quot;,\\&quot;top\\&quot;:\\&quot;151px\\&quot;,\\&quot;left\\&quot;:\\&quot;86px\\&quot;,\\&quot;zIndex\\&quot;:\\&quot;1\\&quot;,\\&quot;svg\\&quot;:\\&quot;&lt;svg width=\\\\\\&quot;54.9375\\\\\\&quot; height=\\\\\\&quot;27.09375\\\\\\&quot; viewBox=\\\\\\&quot;0 0 54.9375 27.09375\\\\\\&quot; xmlns=\\\\\\&quot;http:\/\/www.w3.org\/2000\/svg\\\\\\&quot; xmlns:xlink=\\\\\\&quot;http:\/\/www.w3.org\/1999\/xlink\\\\\\&quot;&gt;&lt;g id=\\\\\\&quot;0.7882792934370437\\\\\\&quot;&gt;&lt;text fill=\\\\\\&quot;#FF0000\\\\\\&quot; stroke=\\\\\\&quot;none\\\\\\&quot; stroke-width=\\\\\\&quot;0\\\\\\&quot; stroke-linecap=\\\\\\&quot;round\\\\\\&quot; stroke-linejoin=\\\\\\&quot;round\\\\\\&quot; x=\\\\\\&quot;\\\\\\&quot; y=\\\\\\&quot;\\\\\\&quot; text-anchor=\\\\\\&quot;start\\\\\\&quot; font-size=\\\\\\&quot;24px\\\\\\&quot; font-family=\\\\\\&quot;arial\\\\\\&quot; data-textcurve=\\\\\\&quot;1\\\\\\&quot; data-itemzoom=\\\\\\&quot;1 1\\\\\\&quot; data-textspacing=\\\\\\&quot;0\\\\\\&quot;&gt;&lt;textPath xmlns:xlink=\\\\\\&quot;http:\/\/www.w3.org\/1999\/xlink\\\\\\&quot; xlink:href=\\\\\\&quot;http:\/\/138.68.62.219\/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\\\\&quot;&gt;&lt;tspan dy=\\\\\\&quot;0\\\\\\&quot;&gt;Hello&lt;\/tspan&gt;&lt;\/textPath&gt;&lt;\/text&gt;&lt;\/g&gt;&lt;defs&gt;&lt;path id=\\\\\\&quot;textPath-item-0\\\\\\&quot; d=\\\\\\&quot;M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\\\\&quot;&gt;&lt;\/path&gt;&lt;\/defs&gt;&lt;\/svg&gt;\\&quot;,\\&quot;rotate\\&quot;:0,\\&quot;text\\&quot;:\\&quot;Hello\\&quot;,\\&quot;color\\&quot;:\\&quot;#FF0000\\&quot;,\\&quot;fontFamily\\&quot;:\\&quot;arial\\&quot;,\\&quot;align\\&quot;:\\&quot;center\\&quot;,\\&quot;outlineC\\&quot;:\\&quot;none\\&quot;,\\&quot;outlineW\\&quot;:0}}

当我用 php 读取这些数据时,我看到的是 ($tmp=$products[0]['design_file']; print_r($tmp);):

"{\\"front\\":{\\"0\\":{\\"type\\":\\"text\\",\\"width\\":\\"55px\\",\\"height\\":\\"27px\\",\\"top\\":\\"151px\\",\\"left\\":\\"86px\\",\\"zIndex\\":\\"1\\",\\"svg\\":\\"<svg width=\\\\\\"54.9375\\\\\\" height=\\\\\\"27.09375\\\\\\" viewBox=\\\\\\"0 0 54.9375 27.09375\\\\\\" xmlns=\\\\\\"http:\/\/www.w3.org\/2000\/svg\\\\\\" xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\"><g id=\\\\\\"0.7882792934370437\\\\\\"><text fill=\\\\\\"#FF0000\\\\\\" stroke=\\\\\\"none\\\\\\" stroke-width=\\\\\\"0\\\\\\" stroke-linecap=\\\\\\"round\\\\\\" stroke-linejoin=\\\\\\"round\\\\\\" x=\\\\\\"\\\\\\" y=\\\\\\"\\\\\\" text-anchor=\\\\\\"start\\\\\\" font-size=\\\\\\"24px\\\\\\" font-family=\\\\\\"arial\\\\\\" data-textcurve=\\\\\\"1\\\\\\" data-itemzoom=\\\\\\"1 1\\\\\\" data-textspacing=\\\\\\"0\\\\\\"><textPath xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\" xlink:href=\\\\\\"http:\/\/138.68.62.219\/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\\\\"><tspan dy=\\\\\\"0\\\\\\">Hello<\/tspan><\/textPath><\/text><\/g><defs><path id=\\\\\\"textPath-item-0\\\\\\" d=\\\\\\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\\\\"><\/path><\/defs><\/svg>\\",\\"rotate\\":0,\\"text\\":\\"Hello\\",\\"color\\":\\"#FF0000\\",\\"fontFamily\\":\\"arial\\",\\"align\\":\\"center\\",\\"outlineC\\":\\"none\\",\\"outlineW\\":0}}

如果我这样做:

$tmp=$products[0]['design_file'];
$info=json_decode($tmp);
print_r($info);

它给了我Null。 我怎样才能让我的数组从那个混乱中恢复过来?

Javascript 发送:

jQuery(document).triggerHandler( "before.addtocart.design", datas);
var finaldata=JSON.stringify(datas);
    jQuery.ajax({
    url: 'index.php?route=checkout/cart/add', //URL TO CONTROL FUNCTION add()
    type: 'post',
    data: 'product_id=' + product_id + '&quantity=' + q+"&option['options']="+finaldata,

读入 PHP

if (isset($this->request->post['option'])) {
            $option = array_filter($this->request->post['option']);

通过查看

$data['products'][] = array(
                'design_file'=>$product['design_file'],
...
$tmp=$products[0]['design_file'];

【问题讨论】:

  • 您的问题是您没有将您真正想要的内容保存在 MySQL 中,但有些内容已转义。所以问题是:为什么?我们需要您的代码...不要试图解决症状,解决原因!
  • 一开始你是如何发送这些数据的?
  • @arkascha @rocket-hazmat .ok thereis js var finaldata=JSON.stringify(datas); data: 'product_id=' + product_id + '&amp;quantity=' + q+"&amp;option['options']="+finaldata, //in ajax body 保存到 mysql if (isset($this-&gt;request-&gt;post['option'])) { $option = array_filter($this-&gt;request-&gt;post['option']);//further standart opencart code 这就是我的阅读方式:$data['products'][] = array( 'design_file'=&gt;$product['design_file'] 在 tpl 文件中 $tmp=$products[0]['design_file'];
  • 抱歉,您不希望任何人能够阅读该内容,对吗?请在问题本身中添加所有相关信息。您的问题下方有一个edit 按钮。 使用它。
  • 您可能使用的 PHP 版本仍然具有 magic_quotes,这意味着您使用的是高度过时/过时的 php。

标签: php mysql json


【解决方案1】:

您可能存储了格式错误的 JSON 数据。看看这些页面(一些有用的资源来自 PHP 文档):

PS.:第二个链接与json_encode中使用的options参数有关。您可以按照自己的方式组合这些值,就像下面的示例一样:

$obj = [
  "prop1" => 123,
  "prop2" => [
    "prop2.1" => "Value A",
    "prop2.2" => [1, 2, 3, 4, 5]
  ]
];

$jsonContent = json_encode($obj, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);

顺便说一句,您仍然可以使用$.ajax 函数调用中的contentType: 'application/json' 设置将您的JavaScript 对象发送到服务器。这将使您能够将来自该 HTTP 请求的请求数据作为 PHP 对象来处理,因为它确实应该如此。

使用 PHP 序列化您的 JSON 内容,以便将其存储在数据库中,而不是直接从请求属性中绕过字符串。

【讨论】:

  • 我按照你说的做了,它成功了,但是对于第一个 lvl,`\"` 还在,我无法解码整个对象$tmp=$products[0]['design_file']; $info=json_decode($tmp,true );//WORKS $info=json_decode(stripslashes($info['options']));//DOESNT
  • 您在 json 中包装数据时遇到了正确的麻烦。如果你有兴趣var finaldata={ 'product_id': product_id, 'quantity':q, 'option':{ 'options':datas// I HAD JSON.stringify(datas) }
【解决方案2】:

这里确实有些混乱。首先,您的初始 JSON 无效。不知何故,它已经有了额外的斜线。

在这里查看:Issue with JSON.stringify adding a extra \ and "" to my Json object。 也许在这里:JSON.stringify escaping without need

在 JSON 字符串的最后也缺少}(应该是}}} 三个右括号)。

其次,我猜在您将其插入数据库之前,您已将其转义(这就是您获得更多斜线的方式)并且您执行了htmlspecialchars()。考虑到这一点,这就是你如何在对象中解析它。

$info = json_decode(stripslashes(htmlspecialchars_decode($tmp.'}')));

【讨论】:

  • 错过了 '{' 可能是我的错,我已经重新设计了我在 js 中包装数据的方式,var finaldata={ 'product_id': product_id, 'quantity':q, 'option':{ 'options':JSON.stringify(datas) } } data: finaldata, 在我能够做到这一点之后&lt;pre&gt; &lt;?php $tmp=$products[0]['design_file']; $info=json_decode($tmp,true ); $info=stripslashes(stripslashes(stripslashes($info['options']))); print_r($info); ?&gt; &lt;/pre&gt; 结果:
  • 你也应该htmlspecialchars_decode(),因为&amp;quot;中编码的双引号。但这不是真正的解决方案...
  • 在我解码根 json 之后,剩下的很少了,我设法解决了这个问题:$info=json_decode($tmp,true ); print_r(($info['options'])); 结果{"product_id":"176","colors":{"0":"000000"},"print":{"sizes":"{\"front\":{\"width\"... 即使使用htmlspecialchars_decode,我现在也无法以你的方式解码这个字符串
  • 好吧,我实际上只是从你的答案{\\&amp;quot;front\\&amp;quot;:{\\&amp;quot;0\\&amp;...(这个)中复制了JSON字符串,把它放在变量$tmp中,然后写了这个字符串$info = json_decode(stripslashes(htmlspecialchars_decode($tmp.'}')))‌​;,这给了我有效的目的。现在,对不起,我不知道您的代码中发生了什么。也许您应该在问题中添加更多代码示例。有没有考虑关闭magic_quotes或者升级PHP?
  • 我刚刚在 JS 中发现了错误,这很愚蠢,我的错...如果您有兴趣 var finaldata={ 'product_id': product_id, 'quantity':q, 'option':{ 'options':datas// I HAD JSON.stringify(datas) } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多