本文簡要介紹ruby語言中 JSON.load 的用法。
用法
load(source, proc = nil, options = {}) → object
返回通過解析給定的 source 創建的 Ruby 對象。
-
參數
source必須是或可轉換為字符串:-
如果
source響應實例方法to_str,則source.to_str成為源。 -
如果
source響應實例方法to_io,則source.to_io.read成為源。 -
如果
source響應實例方法read,則source.read成為源。 -
如果以下兩個都為真,則 source 變為字符串
'null':-
選項
allow_blank指定一個真實值。 -
如上所述,源是
nil或空字符串''。
-
-
否則,
source仍然是源。
-
-
參數
proc如果給定,則必須是接受一個參數的 Proc。它將被每個結果遞歸調用(深度優先順序)。請參閱下麵的詳細信息。注意:此方法旨在序列化來自受信任用戶輸入的數據,例如來自您自己的數據庫服務器或您控製下的客戶端,允許不受信任的用戶將JSON源傳遞給它可能是危險的。 -
參數
opts(如果給定)包含用於解析的選項哈希。請參閱解析選項。可以通過方法JSON.load_default_options=更改默認選項。
當沒有給出proc時,如上修改source並返回parse(source, opts)的結果;見 parse 。
以下示例的來源:
source = <<-EOT
{
"name": "Dave",
"age" :40,
"hats": [
"Cattleman's",
"Panama",
"Tophat"
]
}
EOT
加載字符串:
ruby = JSON.load(source)
ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
加載一個 IO 對象:
require 'stringio'
object = JSON.load(StringIO.new(source))
object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
加載一個文件對象:
path = 't.json'
File.write(path, source)
File.open(path) do |file|
JSON.load(file)
end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
當 proc 給出時:
-
如上所述修改
source。 -
通過調用
parse(source, opts)獲取result。 -
遞歸調用
proc(result)。 -
返回最終結果。
例子:
require 'json'
# Some classes for the example.
class Base
def initialize(attributes)
@attributes = attributes
end
end
class User < Base; end
class Account < Base; end
class Admin < Base; end
# The JSON source.
json = <<-EOF
{
"users": [
{"type": "User", "username": "jane", "email": "jane@example.com"},
{"type": "User", "username": "john", "email": "john@example.com"}
],
"accounts": [
{"account": {"type": "Account", "paid": true, "account_id": "1234"}},
{"account": {"type": "Account", "paid": false, "account_id": "1235"}}
],
"admins": {"type": "Admin", "password": "0wn3d"}
}
EOF
# Deserializer method.
def deserialize_obj(obj, safe_types = %w(User Account Admin))
type = obj.is_a?(Hash) && obj["type"]
safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
end
# Call to JSON.load
ruby = JSON.load(json, proc {|obj|
case obj
when Hash
obj.each {|k, v| obj[k] = deserialize_obj v }
when Array
obj.map! {|v| deserialize_obj v }
end
})
pp ruby
輸出:
{"users"=>
[#<User:0x00000000064c4c98
@attributes=
{"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
#<User:0x00000000064c4bd0
@attributes=
{"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
"accounts"=>
[{"account"=>
#<Account:0x00000000064c4928
@attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
{"account"=>
#<Account:0x00000000064c4680
@attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
"admins"=>
#<Admin:0x00000000064c41f8
@attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
相關用法
- Ruby JSON.load_file!用法及代碼示例
- Ruby JSON.load_default_options用法及代碼示例
- Ruby JSON.load_file用法及代碼示例
- Ruby JSON.generator用法及代碼示例
- Ruby JSON.dump_default_options用法及代碼示例
- Ruby JSON.generate用法及代碼示例
- Ruby JSON.state用法及代碼示例
- Ruby JSON.pretty_generate用法及代碼示例
- Ruby JSON.dump用法及代碼示例
- Ruby JSON.parse用法及代碼示例
- Ruby JSON.fast_generate用法及代碼示例
- Ruby JSON.parse!用法及代碼示例
- Ruby JSON.parser用法及代碼示例
- Ruby JSON.create_id=用法及代碼示例
- Ruby JSON模塊用法及代碼示例
- Ruby JSON[object]用法及代碼示例
- Ruby Time tv_sec用法及代碼示例
- Ruby MatchData.pre_match用法及代碼示例
- Ruby Symbol capitalize用法及代碼示例
- Ruby Matrix lup()用法及代碼示例
- Ruby SizedQueue clear()用法及代碼示例
- Ruby Object.instance_variable_get用法及代碼示例
- Ruby Spotter.spot_op_asgn2_for_name用法及代碼示例
- Ruby Float arg()用法及代碼示例
- Ruby WIN32OLE_METHOD#visible?用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 JSON.load。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
