How do I generate and parse JSON in Ruby? -
I need to create and parse a JSON in Ruby.
In my case, JSON should have a string value in the form of a key, and an array of strings should be in the form of its value.
Something like this ...
JSON: key1 - & gt; {A, b, c} key2 - & gt; {D, e, f} My question: 1.) How can we do this? Is there a JSON parsing library in Ruby or do I have to apply it myself?
2.) I'm coming from Java domain. In Java, we have a map, hash map, hamp, lidapap etc. Do we have the same facility in Ruby?
The JSON you are describing when you create it, will look like this: <">" key1 ": [" a1 ": [" a "," b "," c "]," key2 "You can call it in Ruby using JSON Library and by calling #to_json On a ruby hash: requires 'json': ["d", "e", "f"]} Data_hash = {key1: ['a', 'b', 'c'], key 2: ['d', 'e', 'f']} json_data_string = data_hash.to_json
You can parse it using the same JSON library:
required 'json' json :: parse (json_data_string) Take a look at this.
Comments
Post a Comment