所以我需要这种格式json,到目前为止我有这个
{
query:'Li',
suggestions:['Liberia','Libyan Arab','Liechtenstein','Lithuania'],
data:['LR','LY','LI','LT']
}
query = params[:query]
artists = search_object.map{|x| x["artistName"]}
@all_instances_hash = {}
@all_instances_hash[:query] = query
for instance in artists
@all_instances_hash[:suggestions] = instance
end
respond_to do |format|
format.json { render :json => @all_instances_hash}
end
我使用此代码尝试制作此格式:
这是我的json,似乎只保存最后一个
{"query":"James jones","suggestions":"James Brown & The James Brown Orchestra"}
答案 0 :(得分:2)
待办事项
@all_instances_hash[:suggestions] = artists
而不是
for instance in artists
@all_instances_hash[:suggestions] = instance
end
在第一种情况下,您将数组分配给特定的哈希字段。第二步 - 您重复为该哈希字段分配不同的字符串。