当前位置: 首页 > 工具软件 > JSON for Ruby > 使用案例 >

json内嵌json_JSON宝石

从烈
2023-12-01

json内嵌json

It's easy to jump into parsing and generating JSON in Ruby with the json gem. It provides an API for parsing JSON from text as well as generating JSON text from arbitrary Ruby objects. It's easily the most used JSON library in Ruby.

使用json gem 在Ruby中解析和生成JSON很容易。 它提供了一个API,用于从文本解析JSON以及从任意Ruby对象生成JSON文本。 它很容易成为Ruby中最常用的JSON库。

安装JSON Gem ( Installing the JSON Gem )

On Ruby 1.8.7, you'll need to install a gem. However, in Ruby 1.9.2, the json gem is bundled with the core Ruby distribution. So, if you're using 1.9.2, you're probably all set. If you're on 1.8.7, you'll need to install a gem.

在Ruby 1.8.7上,您需要安装gem。 但是,在Ruby 1.9.2中, json gem与核心Ruby发行版捆绑在一起。 因此,如果您使用1.9.2,则可能已经准备就绪。 如果您使用的是1.8.7,则需要安装gem。

Before you install the JSON gem, first realize that this gem is distributed in two variants. Simply installing this gem with gem install json will install the C extension variant. This requires a C compiler to install, and may not be available or appropriate on all systems. Though if you can install this version, you should.

在安装JSON gem之前,首先要意识到该gem有两种变体。 只需使用gem install json 安装此gem ,即可安装C扩展版本。 这需要安装C编译器 ,并且可能并非在所有系统上都可用或不合适。 虽然如果可以安装此版本,则应该安装。

If you can't install the C extension version, you should gem install json_pure instead. This is the same gem implemented in pure Ruby. It should run everywhere that Ruby code runs, on all platforms and on a variety of interpreters. However, it's considerably slower than the C extension version.

如果无法安装C扩展版本,则应使用gem install json_pure 。 这与纯Ruby中实现的gem相同。 它应该在Ruby代码运行的所有位置,所有平台和各种解释器上运行。 但是,它比C扩展版本要慢得多。

Once installed, there are a few ways to require this gem. A require 'json' (after a prerequisite require 'rubygems' if needed) will require whichever variant is available and will prefer the C extension variant if both are installed. A require 'json/pure' will explicitly require the pure variant, and a require 'json/ext' will explicitly require the C extension variant.

安装后,有几种方法可以使用此gem。 require'json' (如果需要,在前提条件后需要'rubygems' )将要求可用的任何变体,并且如果同时安装了这两种,则将首选C扩展变体。 require'json / pure'将明确要求纯变体,而require'json / ext'将明确要求C扩展变体。

解析JSON ( Parsing JSON )

Before we start, let's define some simple JSON to parse. JSON is typically generated by web applications and can be quite daunting, with deep hierarchies that are difficult to navigate. We'll start with something simple. The top level of this document is a hash, the first two keys hold strings and the last two keys hold arrays of strings.

在开始之前,让我们定义一些简单的JSON进行解析。 JSON通常是由Web应用程序生成的,并且可能令人生畏,具有难以导航的深层次。 我们将从简单的事情开始。 本文档的顶层是哈希,前两个键保存字符串,后两个键保存字符串数组。

So parsing this is quite simple. Assuming this JSON is stored in a file called employees.json, you can parse this into a Ruby object like so.

因此,解析这非常简单。 假设此JSON存储在名为employee.json的文件中,则可以像这样将其解析为Ruby对象。

And this program's output. Note that if you're running this program on Ruby 1.8.7, the order the keys are retrieved from the hash is not necessarily the same order they're inserted. So your output may appear out of order.

以及该程序的输出。 请注意,如果您是在Ruby 1.8.7上运行此程序,则从哈希中检索键的顺序不一定与它们插入的顺序相同。 因此,您的输出可能会出现故障。

The empls object itself is just a hash. Nothing special about it. It has 4 keys, just as the JSON document had. Two of the keys are strings, and two are arrays of strings. No surprises, the JSON was faithfully transcribed in Ruby objects for your perusal.

empls对象本身只是一个哈希。 没什么特别的。 就像JSON文档一样,它具有4个键。 其中两个键是字符串,两个是字符串数组。 毫不奇怪,JSON被忠实地转录在Ruby对象中供您细读。

And that's about all you need to know about parsing JSON. There are some issues that come up, but those will be covered in a later article. For just about every case, you simply read a JSON document from a file or over HTTP and feed it to JSON.parse.

这就是您解析JSON所需的全部知识。 出现了一些问题,但将在以后的文章中介绍。 在几乎每种情况下,您只需从文件或通过HTTP读取JSON文档,然后将其提供给JSON.parse即可

翻译自: https://www.thoughtco.com/json-gem-2908321

json内嵌json

 类似资料: