本文介绍了创建一维Ruby的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要这个:
代替:
到目前为止,我已经有了以下代码:
I have this code so far:
@aeropuertos = ""
f = File.open("./public/aeropuertos/aeropuertos.cvs", "r")
f.each_line { |line|
fields = line.split(':')
if (fields[2] == "N/A")
@line = "(" << fields[1] << ")" << ",," << fields[3] << "," << fields[4]
else
@line = "(" << fields[1] << ")" << "," << fields[2] << "," << fields[3] << "," << fields[4]
end
@aeropuertos += @line << "\n"
}
return CSV.parse(@aeropuertos).to_json
我该怎么办?
推荐答案
@aeropuertos ="
@aeropuertos = ""
f = File.open("./public/aeropuertos/aeropuertos.cvs", "r")
f.each_line { |line|
fields = line.split(':')
if (fields[2] == "N/A")
@line = "(" << fields[1] << ")" << ",," << fields[3] << "," << fields[4]
else
@line = "(" << fields[1] << ")" << "," << fields[2] << "," << fields[3] << "," << fields[4]
end
@aeropuertos += @line << "\n"
}
res = []
CSV.parse(@aeropuertos).each do |c|
res << c.join(',')
end
return res.to_json
这篇关于创建一维Ruby的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!