【发布时间】:2016-01-12 14:30:33
【问题描述】:
我在 Padrino 上使用 Grape 为我的移动应用制作测试 API。
如何指定 JSON 对象的类型?
我是这样做的,但每个返回值都是一个字符串:
module Acme
module Api
class Ping < Grape::API
format :json
get '/user/112132a08s245c/availability_list' do
{
"availability_list"=> [
{
:type=> "OOO",
:from_date=> "21-12-2004",
:to_date=> "21-23-2007",
:all_day=> "false"
},
{
:type=> "WFH",
:from_date=> "21-12-2004",
:to_date=> "21-23-2007",
:all_day=> "false"
}
]
}
end
get '/user/112132a08s245c/issues' do
{
"issues"=> [
{
:issure_id=> "1ab300co221",
:title=> "No water",
:description=> "No water in kitchen",
:severity=> "low",
"location" => {
:lat => "37.4224764",
:lng => "-122.0842499"
}
},
{
:issure_id=> "1ab300co222",
:title=> "No fire",
:description=> "No fire in kitchen",
:severity=> "low",
"location" => {
:lat => "37.4224764",
:lng => "-122.0842499"
}
}
]
}
end
end
end
【问题讨论】:
-
JSON 是一个字符串。它是数据结构的字符串表示形式。
'{"x": 5}' 是一个字符串。 -
我认为您需要阅读the JSON spec。 JSON 将数据序列化为字符串,因为无法在不同语言之间传输对象。当它看到传入的字符串时,解析器知道它必须将它转换回一个对象。