douzhuangxuan3268 2016-09-16 19:01
浏览 335
已采纳

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

I have a problem with encoding and decoding json data. In js I send query with data type 'json', it looks like this:

{\"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:

{\\&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);) :

"{\\"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. How can I get my array back from that mess?

Javascript sending:

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

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'];
  • 写回答

2条回答 默认 最新

  • doushi1900 2016-09-16 19:38
    关注

    You might be storing malformed JSON data. Take a look at these pages (some useful resources right from PHP docs):

    PS.: The second link is related to the options parameter used in json_encode. 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. That will enable you to handle request data from that HTTP request as a PHP object, as it really should be.

    Use PHP to serialize your JSON content in order to store it in database instead of bypassing the string directly from the request attributes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败