简体   繁体   中英

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.

This is the table data在此处输入图像描述

Below code used to fetch data from db and converted into JSON format

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

Executed following solutions but the result was same

  1. use as_json instead of to_json

  2. removed .to_json

I don't think you can use the variable response in a controller action.

It is already used by Rails: https://guides.rubyonrails.org/action_controller_overview.html#the-response-object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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