从jbuilder json删除引号 [英] remove quotes from jbuilder json
问题描述
我是Rails和jbuilder的新手,所以我不确定这是否可行.我正在使用此自动完成jquery插件 http://www.devbridge.com/sourcery/components/jquery-autocomplete/,它期望的响应格式为
I'm new to rails and jbuilder, so I'm not sure if this is doable or not. I'm using this autocomplete jquery plugin http://www.devbridge.com/sourcery/components/jquery-autocomplete/ and the response it expects is to be in the format of
{
query:'Li',
suggestions:['Liberia', 'Libyan Arab Jamahiriya', 'Liechtenstein', 'Lithuania'],
data:['LR', 'LY', 'LI', 'LT']
}
当我尝试使用jbuilder返回json时,我正在返回一个json对象,其中包含键和值,并用引号引起来
When I try to use jbuilder to return json, I'm getting back a json object with both the keys and values in quotes like
{"query":"Comp","suggestions":"['Test Company','Test Company 2','Test company','tester chester','before create test']","data":"['1','2','3','4','5']"}
看来这导致插件无法正常工作,所以有没有办法我可以从返回的json中删除双引号?这是我的jbuilder文件的样子.
It appears that this is causing the plugin to not work, so is there a way I can remove the double quotes from the returned json? Here's what my jbuilder file looks like.
suggestions = ""
data = ""
@companies.each do |company|
suggestions += "'" + company.name + "',"
data += "'" + company.id.to_s + "',"
end
json.query @query
json.suggestions "[" + suggestions[0...-1] + "]"
json.data "[" + data[0...-1] + "]"
谢谢!
推荐答案
我想出了解决方案(或者是我做过的同事;)jbuilder代码已更改为此.无需创建字符串,我需要给json构建器一个数组.
I figured out the solution (or a coworker and I did;) The jbuilder code was changed to this. Rather than creating a string I needed to give the json builder an array.
suggestions = []
data = []
@companies.each do |company|
suggestions.push(company.name)
data.push(company.id)
end
json.query @query
json.suggestions suggestions
json.data data
这篇关于从jbuilder json删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!