HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
This chapter we will introduce how to use the Ruby language to encode and decode JSON objects.
Before using Ruby JSON encoding or decoding data, we need to first install Ruby JSON module. Before installing the module you need to install the Ruby gem, we use Ruby gem install JSON module. However, if you are using the latest version of Ruby, might have installed gem, we will be able to resolve to install Ruby JSON module, use the following command:
$gem install json
Here is the JSON data, the data is stored in input.json file:
{ "President": "Alan Isaac", "CEO": "David Richardson", "India": [ "Sachin Tendulkar", "Virender Sehwag", "Gautam Gambhir", ], "Srilanka": [ "Lasith Malinga", "Angelo Mathews", "Kumar Sangakkara" ], "England": [ "Alastair Cook", "Jonathan Trott", "Kevin Pietersen" ] }
The following Ruby program to parse JSON over document;
#!/usr/bin/ruby require 'rubygems' require 'json' require 'pp' json = File.read('input.json') obj = JSON.parse(json) pp obj
The above examples Implementation of the results:
{"President"=>"Alan Isaac", "CEO"=>"David Richardson", "India"=> ["Sachin Tendulkar", "Virender Sehwag", "Gautam Gambhir"], "Srilanka"=> ["Lasith Malinga ", "Angelo Mathews", "Kumar Sangakkara"], "England"=> ["Alastair Cook", "Jonathan Trott", "Kevin Pietersen"] }