Ruby on rails JSON解析生成url编码的字符串
我以前从未注意到这种行为,但在解析保存在cookie中的JSON编码字符串时,字符串似乎是url编码的,主要症状是空格被加号替换 基本上,它是Rails中的一组flash消息,我不确定问题是否出在使用以下代码编码的编码端:Ruby on rails JSON解析生成url编码的字符串,ruby-on-rails,string,json,Ruby On Rails,String,Json,我以前从未注意到这种行为,但在解析保存在cookie中的JSON编码字符串时,字符串似乎是url编码的,主要症状是空格被加号替换 基本上,它是Rails中的一组flash消息,我不确定问题是否出在使用以下代码编码的编码端: def write_flash_to_cookie cookie_flash = {} flash.each do |key, value| if cookie_flash[key.to_s].nil? or cookie_flash[key.to_s].b
def write_flash_to_cookie
cookie_flash = {}
flash.each do |key, value|
if cookie_flash[key.to_s].nil? or cookie_flash[key.to_s].blank?
cookie_flash[key.to_s]= [value]
else
cookie_flash[key.to_s]<< value
end
end
cookies['flash']= cookie_flash.to_json
flash.clear
end
并显示为:
if($.cookie('flash')) {
var flash= $.parseJSON($.cookie('flash'));
for(type in flash) {
for(message in flash[type]) {
$('#flash').append("<div class=\"" + type + "\">" + flash[type][message] + "</div>");
}
}
}
我看不出我在这里缺少了什么…请尝试字符串末尾的.html\u safe。我相信这在过去帮助了我解决这个问题。尝试过,但没有改变任何事情。问题在于Ruby或Rails在这个例子中,因为to_json使用ActiveSupport cookie支持,URL编码字符串。到目前为止,唯一的解决方案似乎只是用正则表达式替换“+”符号。