如何在rails中将json作为数组呈现,而不使用元标记—(将数据传递到谷歌charts api javascript)

时间:2022-04-30 21:29:11

In rails, this is an api response I'm currently generating as a response from /charts_json/south_carolina.json (for example)

在rails中,这是我当前作为/charts_json/south_carolina的响应生成的api响应。json(例如)

[{"d_s":0,"name":"summerville"},{"d_s":1,"name":"hilton head island"},{"d_s":2,"name":"north myrtle beach"},{"d_s":1,"name":"spartanburg"},{"d_s":12,"name":"greenville, sc"},{"d_s":0,"name":"aiken"},{"d_s":6,"name":"columbia"},{"d_s":4,"name":"myrtle beach"},{"d_s":1,"name":"simpsonville"},{"d_s":1,"name":"lancaster, sc"},{"d_s":0,"name":"north augusta"},{"d_s":0,"name":"sumter"},{"d_s":0,"name":"rock hill"},{"d_s":1,"name":"beaufort, sc"},{"d_s":1,"name":"mount pleasant"},{"d_s":21,"name":"charleston"},{"d_s":1,"name":"clemson"},{"d_s":1,"name":"anderson, sc"}]

Now what I need to do is render the above like this, as a json document

现在我需要做的是将上面的内容呈现为json文档

[['0', 'summerville'], ['1', 'hilton head island'], ...etc... ]

For the benefit of the SO community and the clarification of the reader I'll include all the code I'm going to be using to make this work if and when I get this last thing handled

为了社区的利益和读者的澄清,我将包括所有的代码,我将使用这些代码来完成这个工作,如果我把最后一件事处理好。

In addition to my charts_controller, I generated a charts_json_controller for responding to json requests--- an example of a controller method in that controller (this is a bit clunky but its ok for now as long as I get functionality)

除了charts_controller,我还生成了一个charts_json_controller,用于响应json请求——这是该控制器中一个控制器方法的示例(这有点笨拙,但只要我有了功能就没问题)

def south_carolina
    @locations = Location.find(1687).descendants #used acts_as_tree gem
    respond_to do |format|
      format.json { render json: @location.as_json(only: [:d_s, :name])}
end

In the view (cross section)

在视图中(横截面)

function drawMarkersMap() {
    var data = google.visualization.arrayToDataTable([
      ['Startups', 'Location'],
      $.ajax({url: '/charts_json/south_carolina', dataType: 'json'})
  ]);

1 个解决方案

#1


1  

Not sure if I'm understanding correctly, but here's a way to get the json as an array instead of a hash.

我不确定是否理解正确,但这里有一种方法可以将json作为数组而不是散列。

define this method for Location

为位置定义此方法

class Location
  def self.as_json_array
    as_json(only: [:d_s, :name]).collect { |l| [l[:d_s], l[:name]] }
  end
end

You could make this more general-purpose if you necessary, but I want to make sure I'm understanding your requirements first.

如果需要,您可以使这个功能更通用,但是我想确保我首先理解了您的需求。

Then just use that method instead of as_json in your render line.

然后在呈现行中使用该方法而不是as_json。

Also, it sounds like you know this, but you really should just use the same controller and put any custom code for different formats in your respond_to block.

而且,听起来您似乎知道这一点,但是您确实应该使用相同的控制器,并将不同格式的自定义代码放入respond_to块中。

#1


1  

Not sure if I'm understanding correctly, but here's a way to get the json as an array instead of a hash.

我不确定是否理解正确,但这里有一种方法可以将json作为数组而不是散列。

define this method for Location

为位置定义此方法

class Location
  def self.as_json_array
    as_json(only: [:d_s, :name]).collect { |l| [l[:d_s], l[:name]] }
  end
end

You could make this more general-purpose if you necessary, but I want to make sure I'm understanding your requirements first.

如果需要,您可以使这个功能更通用,但是我想确保我首先理解了您的需求。

Then just use that method instead of as_json in your render line.

然后在呈现行中使用该方法而不是as_json。

Also, it sounds like you know this, but you really should just use the same controller and put any custom code for different formats in your respond_to block.

而且,听起来您似乎知道这一点,但是您确实应该使用相同的控制器,并将不同格式的自定义代码放入respond_to块中。