简体   繁体   English

从 ruby 转换为 JSON 时限制反斜杠

[英]restrict backslash while convering to JSON from ruby

I am working on a ruby API's.This API fetching db value and converted into a JSON format.我正在研究 ruby API。这个 API 获取 db 值并转换为 JSON 格式。

This is the table data这是表格数据在此处输入图像描述

Below code used to fetch data from db and converted into JSON format下面的代码用于从数据库中获取数据并转换为 JSON 格式

def get_data
       
        response= data.select(:xx, :xxx, :xxx, :xxx ).where(:id => xxx1,:xx1 => xx1)
        if response.empty?
            respond_to do |format|
                msg = {:message => "No records found"}
                format.json { render :json => msg }
            end
        else
            respond_to do |format|
                format.json { render json: response.to_json }
            end
        end
    end

Now am getting the response like below现在得到如下响应

在此处输入图像描述

How can i remove the slash from the response?如何从响应中删除斜杠? Thanks in advance.提前致谢。

Note: I was tested this API's from POSTMAN注意:我从POSTMAN测试了这个 API

Executed following solutions but the result was same执行了以下解决方案,但结果相同

  1. use as_json instead of to_json使用as_json而不是to_json

  2. removed .to_json删除.to_json

I don't think you can use the variable response in a controller action.我认为您不能在 controller 操作中使用可变response

It is already used by Rails: https://guides.rubyonrails.org/action_controller_overview.html#the-response-object它已经被 Rails 使用: https://guides.rubyonrails.org/action_controller_overview.html#the-response-object

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

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