我有这样的JSON:
[
{
"id": 1,
"createdAt": "2014-05-08T18:05:09-03:00",
"updatedAt": "2014-05-08T18:05:09-03:00",
},
{
"id": 2,
"createdAt": "2014-05-08T18:08:39-03:00",
"updatedAt": "2014-05-08T18:08:39-03:00",
}
]
我试图将此返回值映射为Virtus model。我的模型就像:
class OrdersCollection
include Virtus.model
attribute :list, Array[OrderCollectionItem]
end
class OrderCollectionItem
include Virtus.model
attribute :id
end
list
属性始终为空,我无法找到原因。
有人可以帮我理解如何以更好的方式映射它?
它不断回复我:NoMethodError. Expected response.body to respond to #to_hash
答案 0 :(得分:2)
您的JSON格式不正确。您没有在JSON中定义list属性,因此Virtus不知道如何正确映射到它。尝试将其格式化为:
{
"list": [
{
"id": 1,
"createdAt": "2014-05-08T18:05:09-03:00",
"updatedAt": "2014-05-08T18:05:09-03:00",
},
{
"id": 2,
"createdAt": "2014-05-08T18:08:39-03:00",
"updatedAt": "2014-05-08T18:08:39-03:00",
}
]
}