ostruct icon indicating copy to clipboard operation
ostruct copied to clipboard

#<NoMethodError: undefined method `encode' for OpenStruct:Class>

Open prateeksen opened this issue 1 year ago • 1 comments

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 avatar Mar 14 '24 08:03 prateeksen

@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))

shetty-tejas avatar Jul 17 '24 04:07 shetty-tejas