创建一维Ruby的JSON [英] Create JSON of one dimension Ruby

查看:137
本文介绍了创建一维Ruby的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这个:

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

["(GKA) GOROKA, GOROKA, PAPUA NEW GUINEA"]

代替:

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

[ [ "(GKA)", "GOROKA", "GOROKA", "PAPUA NEW GUINEA" ] ]

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆