简体   繁体   English

如何使用特殊字符(“ \\\\”)json_decode字符串

[英]How do I json_decode string with special chars (“ \\ ” )

I have a problem with encoding and decoding json data. 我在编码和解码json数据时遇到问题。 In js I send query with data type 'json', it looks like this: 在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}}

Then I save it to cell in MySQL , it looks like this: 然后将其保存到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}}

When I read with php this data that's what I see ( $tmp=$products[0]['design_file']; print_r($tmp); ) : 当我用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}}

If I do this: 如果我这样做:

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

It gives me Null . 它给了我Null How can I get my array back from that mess? 我如何才能从混乱中找回阵列?

Javascript sending: 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,

Read in Php 阅读PHP

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

Pass to view 通过查看

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

You might be storing malformed JSON data. 您可能正在存储格式错误的JSON数据。 Take a look at these pages (some useful resources right from PHP docs): 看一下这些页面(PHP文档中的一些有用资源):

PS.: The second link is related to the options parameter used in json_encode . PS .:第二个链接与json_encode使用的options参数有关。 You might compose those values the way you want, just like the example below: 您可以按照需要的方式组合这些值,如以下示例所示:

$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);

By the way, you still can send your JavaScript objects to the server using a contentType: 'application/json' setting in $.ajax function call. 顺便说一句,您仍然可以使用$.ajax函数调用中的contentType: 'application/json'设置将JavaScript对象发送到服务器。 That will enable you to handle request data from that HTTP request as a PHP object, as it really should be. 这将使您能够像实际那样将来自HTTP请求的请求数据作为PHP对象处理。

Use PHP to serialize your JSON content in order to store it in database instead of bypassing the string directly from the request attributes. 使用PHP序列化JSON内容,以便将其存储在数据库中,而不是直接从请求属性中绕过字符串。

Something is really messed up here. 这里真的有些混乱。 First, your initial JSON is not valid. 首先,您的初始JSON无效。 Somehow it is already has extra slashes. 不知何故,它已经有多余的斜线。

Check here: Issue with JSON.stringify adding a extra \\ and "" to my Json object . 检查这里: JSON.stringify在我的Json对象中添加额外的\\和“”的问题 Maybe here: JSON.stringify escaping without need 也许在这里: JSON.stringify无需转义

Also there is missing } in the very end of JSON string (it should be }}} three closing brackets). JSON字符串的末尾也缺少} (应该是}}}三个右括号)。

Secondly, I guess before you inserted it in database you escaped it (so thats how you got even more slashes) and you did htmlspecialchars() . 其次,我猜想在将其插入数据库之前,先对其进行了转义(这样就可以得到更多的斜线),并且还执行了htmlspecialchars() Considering this, thats how you can parse it in object. 考虑到这一点,这就是如何在对象中解析它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM