简体   繁体   中英

How unescape strings in json with ruby?

Here is json string:

 {
    "app":"${host}\":\"${post}"
 }

How unwrap strings? Eg how to produce this:

{
    app:${host}":"${post}
}

Leading and trailing double quotes are deleted, escaped double quotes become unescaped.

How to do this with ruby?

Also

In java I can use appache commons StringEscapeUtil.unescapeJson Is there analog in ruby?

Important

  1. yep, the result will not be valid json file. This is expected.
  2. I can use something like string.replace("\\"", "") and string.replace("\\\\\\"", "") but it would be nice to use some out-of-the-box function (if it is).
require 'json'

str =<<'END_OF_STRING'
{
    "app":"${host}\":\"${post}"
}
END_OF_STRING

puts str
puts str.gsub(/[\\]" | ["]/x, '\"' => '"', '"' => '')

--output:--
{
    "app":"${host}\":\"${post}"
}
{
    app:${host}":"${post}
}

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