【问题标题】:Need to remove backslashes from json value需要从 json 值中删除反斜杠
【发布时间】:2018-02-17 01:07:26
【问题描述】:

我需要从 codeigniter 中删除 \, 这是我的输出:

{
    "id": "3496",
    "label": "B\\'less S\\'On Chicken Leg 200-240g",
    "sku": "",
    "cat_id": "72",
    "slug": "b-less-s-on-chicken-leg-200-240g",
    "product_min_order_qty": "1",
    "product_unit_type": "pkt",
    "alter_unit_type": "",
    "product_short_desc": null,
    "product_long_desc": "B\\'less S\\'On Chicken Leg 200-240g\r\n\r\n8 X 2kg / ctn",
    "image": null
}

下面是我的代码:

$product_res = $this->Mydb->get_all_records('*', 'table_name');
$responseData = array("status" => 'ok',"categories" => $category_res,"products" => $product_res,"search_count" => $search_count, "Response" => array("response" => "Success", "response_code" => 1));    
$this->output->set_content_type('application/json;'); 
$this->output->set_header('Access-Control-Allow-Origin: *');
return $this->output->set_output(json_encode($responseData,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));

【问题讨论】:

  • str_replace 怎么样?
  • 为什么?后腰带正在逃逸
  • @TobiasF。需要删除阵列...!我试过 str_replace,stripslashes , mysqli_real_escape_string 但没有什么对我有用

标签: php json web-services codeigniter codeigniter-3


【解决方案1】:

只需使用stripslashes 函数

return $this->output->set_output(stripslashes(json_encode($responseData,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)));

【讨论】:

  • SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 155 column 80 of JSON data 它抛出了这个错误......!!
  • @fafpoontha 你能显示$responseData的示例内容吗?
【解决方案2】:

试试这个

var finalData = str.replace(/\\/g, "");

【讨论】:

    【解决方案3】:

    试试修剪功能

    $responseData = array("status" => 'ok',"categories" => trim($category_res),"products" => trim($product_res),"search_count" => $search_count, "Response" => array("response" => "Success", "response_code" => 1));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2020-05-11
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      相关资源
      最近更新 更多