简体   繁体   English

从轨道上 ruby 中的字符串中删除斜线

[英]Remove slash from string in ruby on rails

I need to remove "\" from below string我需要从下面的字符串中删除“\”

{\"MACAddress\":\"74:5E:78\",\"DeviceName\":\"Connected_Device\"}

Response should be回应应该是

{"MACAddress":"74:5E:78","DeviceName":"Connected_Device"}

I need to check if string includes "\n",i need to add validation to remove "\"我需要检查字符串是否包含“\n”,我需要添加验证以删除“\”

Can you please help how to handle this in rails?你能帮我在rails中处理这个吗?

Currently i am using httpparty below code目前我在下面的代码中使用 httpparty

        reqType = params['reqType']
         payLoadData = params['payLoadData']

        p "PAYLOAD DATA-------------- #{payLoadData}"
        if reqType == "post"
          start = Time.now
        url=params['url']
        body_param= device 
        p "payLoadData-------------- #{body_param}"

        response = HTTParty.post(url,
         :body => body_param,
         :headers => {'Content-Type' => 'application/json','User-Agent'=> 'Auto',"Authorization" => 'Basic=='})
        result_hash["response"].push({"body": response.body.to_s, "response_time": response_time.to_s})
        result_hash["status"].push(response.code)


JSON.parse("{\"MACAddress\":\"74:5E:78\",\"DeviceName\":\"Connected_Device\"}")

It should do the trick它应该可以解决问题

The response that you get from your Ajax call is a hash in JSON format.您从 Ajax 调用获得的响应是 JSON格式的 hash。

Just use a JSON parser to translate the JSON string into a Ruby hash:只需使用 JSON 解析器将 JSON 字符串转换为 Ruby Z0800FC577294C934E0BZ58AD28

require 'json'

pay_load = params['payLoadData']
device = JSON.parse(pay_load)

device['MACAddress']
#=> "74:5E:78"
device['DeviceName']
#=> "Connected_Device"

When you just want to output the hash a simple puts device or a <%= device %> (depending on your context) should work.当您只想 output hash 时,一个简单的puts device<%= device %> (取决于您的上下文)应该可以工作。 Because in both cases to_s is called on the hash internally.因为在这两种情况下to_s都会在内部调用 hash。

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

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