我有一个JSON响应,它是一个哈希数组:
[{"project" => {"id" => 1, "name" => "Internal"},
{"project" => {"id" => 2, "name" => "External"}}]
我的代码看起来像这样:
client = HTTP::Client.new(url, ssl: true)
response = client.get("/projects", ssl: true)
projects = JSON.parse(response.body) as Array
这给了我一个数组,但似乎需要将元素强制转换才能使用它们,否则会出现
undefined method '[]' for Nil (compile-time type is (Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)))
错误。我尝试使用
as Array(Hash)
,但是这会得到can't use Hash(K, V) as generic type argument yet, use a more specific type
错误。如何指定类型?