A simple tutorial on JSON Processing Using Ruby, and a json tutorial on ruby Processing

Source: Internet
Author: User

A simple tutorial on JSON Processing Using Ruby, and a json tutorial on ruby Processing

Environment Configuration

Before using Ruby to encode or decode JSON data, we need to install the Ruby JSON module. Before installing this module, you must first install Ruby gem. We will use Ruby gem to install the JSON module. However, if you are using the latest Ruby version, you may have installed the gem. After parsing, you can use the following command to install the Ruby JSON module:

$gem install json

Parse JSON using Ruby

JSON data is stored in the 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 programs are used to parse the preceding JSON files;

#!/usr/bin/rubyrequire 'rubygems'require 'json'require 'pp' json = File.read('input.json')obj = JSON.parse(json) pp obj

The execution result of the above instance is:

{"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"]}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.