mam odpowiedź JSON, który jest tablicą Hash:Jak określić typy danych dla parsowania JSON?
[{"project" => {"id" => 1, "name" => "Internal"},
{"project" => {"id" => 2, "name" => "External"}}]
Mój kod wygląda następująco:
client = HTTP::Client.new(url, ssl: true)
response = client.get("/projects", ssl: true)
projects = JSON.parse(response.body) as Array
To daje mi tablicę, ale wydaje się, muszę typecast elementy faktycznie użyj ich inaczej, otrzymam undefined method '[]' for Nil (compile-time type is (Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)))
.
Próbowałem as Array(Hash)
, ale to daje mi can't use Hash(K, V) as generic type argument yet, use a more specific type
.
Jak określić typ?
Pokonałeś mnie przez 1 minutę! – asterite
pojawia się nowa składnia? https://play.crystal-lang.org/#/r/1uhw lub możesz użyć to_i i to_s FWIW. – rogerdpack
To prawda, zaktualizowałem odpowiedź. –