如何在to_json formating之后将单个后斜线保持在Ruby字符串中



我需要编码一些包含URL字符串的哈希。我使用to_json方法,并且需要在每个斜线前面的后斜线(如PHP打印这样的字符串(。例如:

hash = {"url":"http:\/\/example.com\/test"}
hash.to_json

结果是

{:url=>"http:\/\/example.com\/test"}

当我需要(和PHP的JSON_ENCODE带有单个后斜线的字符串(。

{:url=>"http://example.com/test"}

在编码时,将字符串保持在PHP中非常重要。因为带有双重和单次斜线的字符串获得了不同的结果。

upd:问题不在交流中。我需要使用HMAC(SHA384(编码JSON。当我使用URL字符串时,结果在PHP和Ruby上是不同的。如果字符串不包含后斜线,则所有功能都很好...

PHP实现引入了后斜线。JSON使用php的JSON看起来很{"url":"http://example.com/test"},而Ruby的JSON是{"url":"http:\/\/example.com\/test"}

我的歉意,您似乎确实有一个有效的问题。关键是:为什么斜线在JSON中是一个可逃避的角色?及其重复的目标,JSON:为什么向前斜线逃脱?由于允许两种悬而未决的斜线和逃脱的斜线,因此Ruby选择不逃脱它们,而PHP选择逃脱它们,两种方法都是正确的。

(撇开:谈论这一点有些复杂,因为既是字符串字面的逃生字符又是JSON字符串。因此,在此答案中,我要注意puts(或echo/print_r(所有的值,要查看没有字符串字面反闪烁的字符串,只有字符串中实际存在的后斜切。(

因此,JSON {"url":"http://example.com/test"}是Ruby Hash { 'url' => 'http://example.com/test' }的代表,其中斜线被逃脱了(如PHP的json_encode所做的那样(。Ruby's to_json' would render that as {" url":" http://example.com/test"}`:

# Ruby
json1 = '{"url":"http://example.com/test"}'
puts json1                        # => {"url":"http://example.com/test"}
puts JSON.parse(json1)            # => {"url"=>"http://example.com/test"}
puts JSON.parse(json1).to_json    # => {"url":"http://example.com/test"}
# PHP
$json1 = '{"url":"http://example.com/test"}';
echo $json1;                           # {"url":"http://example.com/test"}
print_r(json_decode($json1));          # stdClass Object
                                       # (
                                       #     [url] => http://example.com/test
                                       # )
echo json_encode(json_decode($json1)); # {"url":"http://example.com/test"}

另一方面,{"url":"http:\/\/example.com\/test"}(在Ruby和PHP中代表字符串'{"url":"http:\\/\\/example.com\\/test"}'(是Ruby Hash { 'url' => 'http://example.com/test' }的表示,那里有实际的后刺,但斜线没有逃脱。PHP的json_encode将以{"url":"http:\/\/example.com\/test"}渲染此值。

# Ruby
json2 = '{"url":"http:\\/\\/example.com\\/test"}'
puts json2                        # => {"url":"http:\/\/example.com\/test"}
puts JSON.parse(json2)            # => {"url"=>"http:\/\/example.com\/test"}
puts JSON.parse(json2).to_json    # => {"url":"http:\/\/example.com\/test"}
# PHP
$json2 = '{"url":"http:\\/\\/example.com\\/test"}';
echo $json2;                           # {"url":"http://example.com/test"}
print_r(json_decode($json2));          # stdClass Object
                                       # (
                                       #     [url] => http://example.com/test
                                       # )
echo json_encode(json_decode($json2)); # {"url":"http:\/\/example.com\/test"}

PHP json_encode可以选择防止PHP逃脱后斜线的默认值:

# PHP
echo json_encode('/');                         # "/"
echo json_encode('/', JSON_UNESCAPED_SLASHES); # "/"

Ruby没有类似的选择可以强迫斜线逃脱,但是由于斜线在JSON中没有特殊的含义,因此我们可以用/手动替换/

# Ruby
puts '/'.to_json                  # "/"
puts '/'.to_json.gsub('/', '/')  # "/"

如果您不想处理逃脱的后斜切,请在字符串周围使用单引号。

hash = { url: 'http://example.com/test' }
json = hash.to_json
puts json
# => {"url":"http:\/\/example.com\/test"}

只是一个快速提醒:在JSON中,需要逃脱,因为它们被视为控制字符。

这样,当PHP解析此JSON文档时,您将在每个斜线之前获得单个后斜线。

问题背后的问题可能是真正的问题。我不确定,因为您的问题对我来说并不完全清楚,所以我在这里回答了一个猜测/假设。

我的假设是您想与json。

好吧,在这种情况下,您不必有问题(带有后斜线(。

让Ruby .to_json ( json.generate(..((和 JSON.PARSE(..(解决红宝石部分和LET JSON_ENCODE(( JSON_DECODE((求解PHP部分,然后完成。

SO RUBY中:
- 不要使用额外的逃避背部,让 .to_json 为您解决
- 使用字面的URL字符串,您将在浏览器中输入,例如:

hash = {"url":"http://example.com/test"} # hash is a ruby object
puts hash.to_json                        # => {"url":"http://example.com/test"} is JSON (string)

然后在php 中:

var_dump( json_decode('{"url": "http://example.com/test"}') );

给你:

object(stdClass)#1 (1) {
  ["url"]=>
  string(23) "http://example.com/test"
}
var_dump( json_decode('{"url": "http://example.com/test"}') );

给你:

object(stdClass)#1 (1) {
  ["url"]=>
  string(23) "http://example.com/test"
}

注意两个JSON字符串最终在PHP中正确解析,最终作为普通PHP对象

尝试如下

hash = {"url":"http:\/\/example.com\/test"}
hash[:url] = hash[:url].delete("\")
hash.to_json  #"{"url":"http://example.com/test"}"

希望它能帮助您

最新更新