什么是将哈希数据数组转换为特定图表格式数据的最佳方法

时间:2013-09-03 04:36:10

标签: ruby hash backslash gsub amcharts

我有一个从下面的map函数返回的Hashes数组。我正在尝试为图表填充数据(我正在使用 AM图表),它会采用下面提到的特定格式的数据 -

公司记录的样本结构

1.9.3p327 :073 > Company
 => Company(id: integer, name: string, contact_no: integer, email_id: string, website: string, fax_no: integer, created_at: datetime, updated_at: datetime, divisions_count: integer) 

@all_companies = Company.all

地图功能:

@all_companies = @all_companies.map {|each_company| { Country: each_company.name, Visits: each_company.divisions_count} }

以上查询的输出格式

[{:country=>"Samsung", :visits=>8}, {:country=>"Natraj", :visits=>2}, {:country=>"Tupperware", :visits=>5}, {:country=>"Transcen", :visits=>0}, {:country=>"camlin", :visits=>0}]

图表数据输入格式:

  var chartData = [{
      country: "USA",
      visits: 4025
  }, {
      country: "China",
      visits: 1882
  }, {
      country: "Japan",
      visits: 1809
  }, {
      country: "Germany",
      visits: 1322
  }]

我正在使用Ruby 1.9.3-p327。我需要将输出数组转换为特定的图形格式。 。我尝试通过以下步骤进行相同的操作: -

  1.9.3p327 :070 >     @all_companies = @all_companies.to_s.gsub(":","")
 => "[{country=>\"Samsung\", visits=>8}, {country=>\"Natraj\", visits=>2}, {country=>\"Tupperware\", visits=>5}, {country=>\"Transcen\", visits=>0}, {country=>\"camlin\", visits=>0}]" 
1.9.3p327 :071 >     @all_companies = @all_companies.gsub("=>",": ")
 => "[{country: \"Samsung\", visits: 8}, {country: \"Natraj\", visits: 2}, {country: \"Tupperware\", visits: 5}, {country: \"Transcen\", visits: 0}, {country: \"camlin\", visits: 0}]" 
1.9.3p327 :072 > puts @all_companies
[{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]
 => nil 
1.9.3p327 :073 > 

现在,当我尝试在图表中显示数据时,我收到以下语法错误: -

var chartData = [{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]

来自firebug控制台的语法错误现在指向上述数据中的& quot

任何可以帮助我完成最后一步的工作,以便我可以获得所需的图形格式?

问题已更新

我需要以下格式的数据: -

  var chartData = [

      {
          country: "Samsung",
          visits: 8
      },
      {
          country: "Natraj",
          visits: 2
      },
      {
          country: "Tupperware",
          visits: 5
      },
      {
          country: "Transcen",
          visits: 0
      },
      {
          country: "camlin",
          visits: 0
      }

      ]

谢谢。

3 个答案:

答案 0 :(得分:1)

因为您没有打印您的价值。控制台只显示它,双引号被转义 否则你怎么知道我的字符串:“我的”字符串“什么”结束了?

如果你这样做

puts @specific_details.to_s

你不会看到那些双引号被转义。

答案 1 :(得分:1)

  

我需要将输出数组转换为特定的图形格式。

使用Awesome Print Rubygem -

执行此操作
require "awesome_print"

hsh = [{:Country=>"Samsung", :Visits=>8}, {:Country=>"Natraj", :Visits=>2}, 
       {:Country=>"Tupperware", :Visits=>5}, {:Country=>"Transcen", :Visits=>0}, 
      {:Country=>"camlin", :Visits=>0}]

ap hsh,{:index => false}

<强>输出

[
    {
        :Country => "Samsung",
         :Visits => 8
    },
    {
        :Country => "Natraj",
         :Visits => 2
    },
    {
        :Country => "Tupperware",
         :Visits => 5
    },
    {
        :Country => "Transcen",
         :Visits => 0
    },
    {
        :Country => "camlin",
         :Visits => 0
    }
]

答案 2 :(得分:0)

我想通了这个question

这是我为克服&amp; quot语法错误而采取的措施。

在控制器中: -

def company_division_stats
    @all_companies = @companies_data = Company.all 

    @specific_details = @all_companies = @all_companies.map {|each_company| { country: each_company.name, visits: each_company.divisions_count} } 
    #@all_companies = @all_companies.to_s.gsub(":","")
    #@all_companies = @all_companies.gsub("=>",": ")

    respond_to do |format|
      unless @all_companies.nil?
        format.html
        format.json { render json: @specific_details, status: :created } #in the browser url would be:- localhost:3000/company_division_stats.json
      end
    end

在视图中: -

var chartData = <%= @all_companies.to_json.html_safe %>