1

我有一个从下面的 map 函数返回的哈希数组。我正在尝试为图表填充数据(我正在使用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 控制台的语法错误现在指向上述数据中的" 。

有什么解决方法可以帮助我完成最后一步,以便获得所需的图形格式?

问题更新

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

  var chartData = [

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

      ]

谢谢你。

4

3 回答 3

1

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

如果你只是这样做

puts @specific_details.to_s

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

于 2013-09-03T04:42:09.713 回答
1

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

使用Awesome PrintRubygem 执行此操作 -

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
    }
]
于 2013-09-03T04:43:21.610 回答
0

由于这个问题,我想通了。

这就是我为克服" 语法错误所做的事情。

在控制器中: -

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 %>
于 2013-09-03T07:19:02.567 回答