2013-08-02 45 views
1

對不起,我的英語不好。如何在rails上創建根json?

我想找到同樣的問題我的問題。我的問題是json不是根。

這是我的JSON數據:

[ 
{ 
created_at: "2013-07-26T02:49:55Z", 
description: "hgfhgfhf", 
iconapp: null, 
id: 13, 
name: "test1", 
updated_at: "2013-07-26T02:49:55Z", 
}, 
{ 
created_at: "2013-07-26T02:54:40Z", 
description: "sadsadas", 
iconapp: null, 
id: 14, 
name: "asdadas", 
updated_at: "2013-07-26T02:54:40Z", 
} 
] 

,但我想我的JSON數據有這樣的根元素:

- Apps: [ 
{ 
created_at: "2013-07-26T02:49:55Z", 
description: "hgfhgfhf", 
iconapp: null, 
id: 13, 
name: "test1", 
updated_at: "2013-07-26T02:49:55Z", 
}, 
{ 
created_at: "2013-07-26T02:54:40Z", 
description: "sadsadas", 
iconapp: null, 
id: 14, 
name: "asdadas", 
updated_at: "2013-07-26T02:54:40Z", 
} 
] 

Json的根源是 「應用程序」。怎麼辦?

請幫幫我嗎?

回答

1

它是一個配置的東西。默認情況下,根不包含在json中。如果你想讓它應用廣泛將它添加到一個新的初始化時,應用程序啓動將運行該代碼

ActiveRecord::Base.include_root_in_json = true 

:您可以通過啓用它。它記錄在這裏:http://apidock.com/rails/ActiveRecord/Serialization/to_json

+0

cooooooool非常感謝你 – Bustex