-1

我正在使用 rabl 在 Sinatra 中构建和 API。所需的 JSON des 不接受某些节点中的标签使用此 Rabl 模板:

object @user
  attributes :profile_photo => :profile_photo, :name => :first_name, :last_name =>       :last_name
  child :addresses do
  attributes :id 
    child :country do
      attributes :name
    end
  end

我得到以下 JSON:

{
user: {
profile_photo: null,
first_name: "John",
last_name: "Doe",
addresses: [
{
address: {
id: 43,
country: {
name: "Iceland"
}
}
},
{
address: {
id: 44,
country: {
name: "Cambodia"
}
}
},
{
address: {
id: 45,
country: {
name: "North Korea"
}
}
}
]
}
}

我想要得到的是:

{
user: {
profile_photo: null,
first_name: "John",
last_name: "Doe",
addresses: [
{
{
id: 43,
country: {
name: "Iceland"
}
}
},
{
{
id: 44,
country: {
name: "Cambodia"
}
}
},
{
{
id: 45,
country: {
name: "North Korea"
}
}
}
]
}
}

有没有办法在 Rabl 中做到这一点?

4

1 回答 1

0

我必须将 Rabl 配置设置为:

Rabl.configure do |config|
  config.include_json_root = false
  config.include_child_root = false
end

我从另一个堆栈溢出线程中得到了答案:

删除 RABL 中的子根节点

于 2013-04-12T17:35:37.343 回答