有没有办法阻止Ruby的JSON.pretty_generate()方法转义Unicode字符?
我有一个JSON对象如下:
my_hash = {"my_str" : "\u0423"};
Run Code Online (Sandbox Code Playgroud)
正在运行JSON.pretty_generate(my_hash)返回值\\u0423.
有什么方法可以防止这种行为吗?
我的哈希数组:
data = [{:bool => true, :val => 5}, {:bool => false, :val => 9}, {:bool => true, :val => 1}]
Run Code Online (Sandbox Code Playgroud)
我想迭代数据并只检索一个值数组.我可以:
data.map{|x| x[:val] if x[:bool]}
Run Code Online (Sandbox Code Playgroud)
返回:
[5, nil, 1]
Run Code Online (Sandbox Code Playgroud)
但是这个方法需要额外的.compact调用来摆脱nil值.
有没有更好的方法来实现这一目标?