Second Issue json["object"]["contents"].each do |content| content["items"].each do |item| case item["type"] when "image" image_id = item["image_id"] x = item["position"]["x"] # ... when "text" # ... end end end
Second Issue json.fetch("object").fetch("contents").each do |content| content.fetch("items").each do |item| case item.fetch("type") when "image" image_id = item.fetch("image_id") x = item.fetch("position").fetch("x") # ... when "text" # ... end end end
Golang JSON type Point struct { X int `json:"x"` Y int `json:"y"` } type Line struct { P1 Point `json:"p1"` P2 Point `json:"p2"` } func main() { const input = `{"p1":{"x":10,"y":20},"p2":{"x":30,"y":40}}` var line Line json.NewDecoder(strings.NewReader(input)).Decode(&line) fmt.Println(line.P2.Y) //=> 40 }
Crystal JSON class Point JSON.mapping({ x: Int64, y: Int64, }) end class Line JSON.mapping({ p1: Point, p2: Point, }) end input = %({"p1":{"x":10,"y":20},"p2":{"x":30,"y":40}}) line = Line.from_json(input) puts line.p2.y #=> 40
TypeStruct json.object.contents.each do |content| content.items.each do |item| case item when Image image_id = item.image_id x = item.position.x # ... when Text # ... end end end
HashOf class HashOf def initialize(key_type, value_type) @key_type = key_type @value_type = value_type end def ===(h) h.all? do |k, v| @key_type === k && @value_type === v end end end
include Boolean? module Boolean end class TrueClass include Boolean end class FalseClass include Boolean end Boolean === true #=> true Boolean === false #=> true Boolean === Object.new #=> false