创建一维Ruby的JSON

创建一维Ruby的JSON,ruby,json,Ruby,Json,我想要这个: [“(吉卡)戈罗卡,戈罗卡,巴布亚新几内亚 几内亚“] 而不是: [ [ “(GKA)”, “GOROKA”, “GOROKA”, “巴布亚新几内亚” ]] 到目前为止,我有以下代码: @aeropuertos = "" f = File.open("./public/aeropuertos/aeropuertos.cvs", "r") f.each_line { |line| fields = line.split(':') if (fi

我想要这个:

[“(吉卡)戈罗卡,戈罗卡,巴布亚新几内亚 几内亚“]

而不是:

[ [ “(GKA)”, “GOROKA”, “GOROKA”, “巴布亚新几内亚” ]]

到目前为止,我有以下代码:

@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=“”
f=文件.open(“./public/aeropuertos/aeropuertos.cvs”,“r”)
f、 每条{线|
字段=行。拆分(“:”)
如果(字段[2]=“不适用”)

@line=“(”不需要CSV解析器。只需在阅读每一行时创建所需的结构。也就是说,不要在
@aeropuertos
中生成一个大字符串并使用CSV解析器进行解析,而是将
@aeropuertos
生成一个数组,然后将每个
@line
添加到数组中

因此,与此相反:

@aeropuertos += @line << "\n"
@aeropuertos=“”

f=File.open(“./public/aeropuertos/aeropuertos.cvs”,“r”)
f、 每条{线|
字段=行。拆分(“:”)
如果(字段[2]=“不适用”)

@line=“(“为什么手动拆分CSV只是为了生成一个新的CSV字符串,然后由CSV.parse解析?
@aeropuertos << @line
@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