ruby - Hyphens in serialized JSON OpenStruct -
i have json object such as:
"c": { "10-20": 9.0, "0-10": 8.5, "30-end": 5.085714285714286, "20-30": 10.3 }
when convert json serialized object using:
json.parse(response.body, object_class: openstruct)
it gives me:
<openstruct 10-20=0, 0-10=8.5, 30-end=5.085714285714286, 20-30=10.3>
naturally can't accessed c.10-20 don't believe hyphens valid class variable names. so, how access these values?
you can use square brackets hash:
obj["10-20"] #=> 0
of course, if of keys not valid method names anyway, might use hash , not bother openstruct
.
related documentation: openstruct#[]
Comments
Post a Comment