ostruct
ostruct copied to clipboard
#<NoMethodError: undefined method `encode' for OpenStruct:Class>
I am getting the below error when trying to parse a json object into class object.
#<NoMethodError: undefined method `encode' for OpenStruct:Class>
Code I am using:
# json_string = {"latitude":409146138,"longitude":124678384}
object = JSON.parse(json_string, object_class: OpenStruct)
Target Object looks like this:
<Routeguide::Point: latitude: 409146138, longitude: -746188906>
@prateeksen
The above code is working with OpenStruct version 0.3.1 and JSON version 2.6.2.
It's not exactly clear what you're trying to do here. From what I can understand, are you just trying to create an object out of a JSON string into an OpenStruct object?
If so, just check if your versions match, else try this code:
OpenStruct.new(JSON.parse(json_string))