JSON -为Rails嵌套子RABL或JBuilder

时间:2023-01-22 20:39:39

I have objects that look like:

我有这样的东西:

[<ltree_val: "1", contents: "blah">, 
 <ltree_val: "1.1", contents: "blah">,
 <ltree_val: "1.1.1", contents: "blah">,
 <ltree_val: "2", contents: "blah">,
 <ltree_val: "2.1", contents: "blah">]

Where ltree_val determines their tree structure.

ltree_val决定了它们的树结构。

I need to generate something like...

我需要生成一些…

[{ "data" : "1",
  "children" : 
      [{ "data" : "1.1",
         "children" : 
              [{ "data" : "1.1.1" }]
       }]
  },
  { "data" : "2" }]

Where I have children which are determined by an ltree value, which are themselves elements of the same object.

其中的子元素由ltree值决定,ltree值本身就是同一个对象的元素。

If I sort these objects by their ltree value, how can I create nested entries?

如果我按对象的ltree值对这些对象进行排序,我如何创建嵌套条目?

I'm open to either RABL or JBuilder. I'm totally lost.

我对RABL或JBuilder都持开放态度。我完全迷路了。

1 个解决方案

#1


3  

The answer was to use a recursive function...

答案是使用递归函数……

# encoding: UTF-8

def json_ltree_builder( json, ltree_item )
  json.title t( ltree_item.title )
  json.attr do
    json.id ltree_item.id
  end
  json.metadata do
      json.val1 ltree_item.val1
      json.val2 ltree_item.val2
    end
  children = ltree_item.children
  unless children.empty?
    json.children do
      json.array! children do |child|
        json_ltree_builder( json, child )
      end
    end
  end
end

json.array! @menu_items do |menu_item|
  json_ltree_builder( json, menu_item )
end

This builds something like

这个构建类似的

[
  {  "title":"Title 1", 
    "attr" : {
      "id": 111
    },
    "data" : {
      "val1" : "Value 1",
      "val2" : "Value 2"
    },
    "children" : [
      {
        "title":"Child 1", 
        "attr" : {
          "id": 112
        },
        "data" : {
          "val1" : "Value 1",
          "val2" : "Value 2"
        }
      },
      {
        "title":"Child 2", 
        "attr" : {
          "id": 112
        },
        "data" : {
          "val1" : "Value 1",
          "val2" : "Value 2"
        }
      }
    ]
  }
]

#1


3  

The answer was to use a recursive function...

答案是使用递归函数……

# encoding: UTF-8

def json_ltree_builder( json, ltree_item )
  json.title t( ltree_item.title )
  json.attr do
    json.id ltree_item.id
  end
  json.metadata do
      json.val1 ltree_item.val1
      json.val2 ltree_item.val2
    end
  children = ltree_item.children
  unless children.empty?
    json.children do
      json.array! children do |child|
        json_ltree_builder( json, child )
      end
    end
  end
end

json.array! @menu_items do |menu_item|
  json_ltree_builder( json, menu_item )
end

This builds something like

这个构建类似的

[
  {  "title":"Title 1", 
    "attr" : {
      "id": 111
    },
    "data" : {
      "val1" : "Value 1",
      "val2" : "Value 2"
    },
    "children" : [
      {
        "title":"Child 1", 
        "attr" : {
          "id": 112
        },
        "data" : {
          "val1" : "Value 1",
          "val2" : "Value 2"
        }
      },
      {
        "title":"Child 2", 
        "attr" : {
          "id": 112
        },
        "data" : {
          "val1" : "Value 1",
          "val2" : "Value 2"
        }
      }
    ]
  }
]