6.45. Ruby JSON

发布时间 :2023-11-02 23:00:07 UTC      

In this section we will show you how to use the Ruby language to encode and decode JSON objects.

6.45.1. Environment configuration #

Before using Ruby to encode or decode JSON data, we need to install the RubyJSON module. You need to install Ruby gem before installing this module, and we use Ruby gem to install the JSON module. However, if you are using the latest version of Ruby, you may have already installed gem after parsing, we can install the Ruby JSON module using the following command

$gem install json

6.45.2. Parsing JSON using Ruby #

Here is the JSON data, which is stored in the input.json file:

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 is used to parse the above JSON file

Example #

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

The execution result of the above example 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"]
}

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.