Thanks to the previous help this works:
require "json"
struct Person
include JSON::Serializable
getter name : String
getter email : String
end
json_str = %{{"name": "Bar", "email": "bar@foobar.com"}}
prs = Person.from_json(json_str)
p! prs
p! prs.name
p! prs.email
but I don’t know what to do when a field (e.g. Email) in the JSON starts with upper-case:
require "json"
struct Person
include JSON::Serializable
getter name : String
getter Email : String
end
json_str = %{{"name": "Bar", "Email": "bar@foobar.com"}}
prs = Person.from_json(json_str)
p! prs
p! prs.name
p! prs.email
In struct_from_json_upper.cr:7:16
7 | getter Email : String
^
Error: unexpected token: :