Rails,创建树状结构。 JSON 输出

发布于 2024-10-30 19:56:15 字数 931 浏览 0 评论 0原文

我有一个数据表,可以彼此作为父母或孩子,它用字段parent_id处理(我使用的是act_as_tree gem)

第一级项目有0作为parent_id,

可能有无限数量的孩子。我想输出为 JSON。 最终的输出应该是这样的,

{
   "feild1": "dd",
   "filed2": "ee",
   "child" : {
       "feild1": "dd",
        "filed2": "ee",
        } 
   "child" : {
        "feild1": "dd",
        "filed2": "ee",
                 "child" : {
                      "feild1": "dd",
                      "filed2": "ee",
                  } 
        } 

}

到目前为止,

def coa_tree
   @roots = Coa.find(:all, :conditions => ['parent_id = ?', 0])
   @response = @roots

   @roots.each do |root|
     logger.debug "roots each"
      output = root
      root.children.each do |child|
           output = {:child => output, :child => child}

      end

   end
   respond_with(@response)
end

我所拥有的就是这个,显然我什至还没有接近解决问题。如果有人能指出我正确的方向,我将非常感激。也许有一个我不知道的插件可以帮助我解决这个问题。 谢谢。

i have a table of data, which can have each other as parents or childs, its handled with the field parent_id (I am using the act_as_tree gem)

the first level items have 0 as parent_id

there could be an infinite number of children. I want to output as JSON.
the final output shud be something like this

{
   "feild1": "dd",
   "filed2": "ee",
   "child" : {
       "feild1": "dd",
        "filed2": "ee",
        } 
   "child" : {
        "feild1": "dd",
        "filed2": "ee",
                 "child" : {
                      "feild1": "dd",
                      "filed2": "ee",
                  } 
        } 

}

so far i all i have is this

def coa_tree
   @roots = Coa.find(:all, :conditions => ['parent_id = ?', 0])
   @response = @roots

   @roots.each do |root|
     logger.debug "roots each"
      output = root
      root.children.each do |child|
           output = {:child => output, :child => child}

      end

   end
   respond_with(@response)
end

clearly i haven't even come close to solving the problem. If someone could point me to the right direction i would really appreciate it. maybe there is a plugin that i don't know about that would help me solve this.
thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一花一树开 2024-11-06 19:56:15

您的 JSON 示例无效,因为同一对象中有多个具有相同名称的键,但从 ActiveRecord 对象将树结构输出为 JSON 绝对是可能的。

尝试将这样的方法添加到您的模型类中:

class Coa < ActiveRecord::Base
  def to_node
    { "attributes" => self.attributes,
      "children"   => self.children.map { |c| c.to_node }
    }
  end
end

现在您可以使用以下方法以 JSON 形式检索整个树:

root = Coa.find(:first, :conditions => ["parent_id = ?", 0])
root.to_node.to_json

Your JSON example is invalid because there are multiple keys in the same object with the same name, but outputting a tree structure as JSON from an ActiveRecord object is definitely possible.

Try adding a method like this to your model class:

class Coa < ActiveRecord::Base
  def to_node
    { "attributes" => self.attributes,
      "children"   => self.children.map { |c| c.to_node }
    }
  end
end

Now you can retrieve the entire tree as JSON using:

root = Coa.find(:first, :conditions => ["parent_id = ?", 0])
root.to_node.to_json
小兔几 2024-11-06 19:56:15

我要在 Todd Yandell 的答案中添加的唯一一点是,对于“属性”和“子项”没有单独的键可能会很有用。换句话说,虽然 Yandell 的方法将返回如下数据:

{
   "attributes" : {
        "field1": "dd",
        "field2": "ee",
   "children" : [{
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }]
}

您可能希望上述问题中的数据的格式如下:

{
   "field1": "dd",
   "field2": "ee",
   "children" : [{
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }]
}

该格式对于客户端代码需要树格式 JSON 数据且不允许灵活地为父属性和子属性定义不同的键(例如,Sencha Touch 中的 NestedList 组件等)。

为了实现这一目标,在 Yandell 的概念的基础上,我提出了以下建议:

def to_node
  self.attributes.merge({:children => self.children.map { |c| c.to_node }})
end

The only thing I would add to Todd Yandell's answer is that it may be useful to not have separate keys for "attributes" and "children". In other words, while Yandell's method would return data like this:

{
   "attributes" : {
        "field1": "dd",
        "field2": "ee",
   "children" : [{
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }]
}

You may wish for the data in the question, above, to be formatted like this:

{
   "field1": "dd",
   "field2": "ee",
   "children" : [{
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }, {
        "field1": "dd",
        "field2": "ee",
    }]
}

That format is particularly useful with situations where client-side code expects tree-formatted JSON data and does not allow flexibility to define different keys for parent attributes and children (e.g., a NestedList component in Sencha Touch, etc.).

To accomplish this, building on Yandell's concept, I came up with the following:

def to_node
  self.attributes.merge({:children => self.children.map { |c| c.to_node }})
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文