JSON由于其數(shù)據(jù)結(jié)構(gòu)簡單便利,已逐漸成為了互聯(lián)網(wǎng)上的主流數(shù)據(jù)交換的數(shù)據(jù)格式。
在討論嵌套對象(Nested Object)的JSON轉(zhuǎn)換方法之前,我們先看簡單的ruby JSON轉(zhuǎn)換。
首先,ruby對象轉(zhuǎn)換為JSON字符串:
復(fù)制代碼 代碼如下:
class Obj1
def initialize(var1)
@var1 = var1
end
def to_json(*a)
{
"json_class" => self.class,
"data" => {"var1" => @var1}
}.to_json(*a)
end
def self.json_create(json_str)
new(json_str["data"]["var1"])
end
end
obj1 = Obj1.new("i am obj1")
#obj1 to JSON string
json_str = obj1.to_json
puts "JSON string of obj1 = #{json_str}"
#JSON string to obj1
obj11 = JSON.parse(json_str)
puts "ob1 from json string = #{obj11.var1}"
上面代碼我們可以看到,ruby與JSON string之間的轉(zhuǎn)換,關(guān)鍵有三個點:
#引入json庫,才能有下面兩個方法,json是通過open class的方式,給Hash對象加上了to_json(*a)方法,關(guān)于ruby的open class參考支持Open Class特性的編程語言中的開閉原則(Open-Closed Principle)
1)require ‘json'
#定義對象轉(zhuǎn)為JSON string的to_json(*a)方法,其實現(xiàn)是使用Hash對象的to_json(*a)方法
2)def to_json(*a)
#定義從JSON string構(gòu)造對象的json_create方法,此方法是類方法
3)def self.json_create(json_str)
上面三點是Ruby中實現(xiàn)JSON string互相轉(zhuǎn)換的基本要求。
代碼運行結(jié)果為:
復(fù)制代碼 代碼如下:
JSON string of obj1 = {"json_class":"Obj1","data":{"var1":"i am obj1"}}
ob1 from json string = i am obj1
現(xiàn)在我們來看嵌套對象的JSON string轉(zhuǎn)換:
復(fù)制代碼 代碼如下:
#!/usr/local/ruby/bin/ruby
require 'json'
class Obj1
def initialize(var1)
@var1 = var1
end
def to_json(*a)
{
"json_class" => self.class,
"data" => {"var1" => @var1}
}.to_json(*a)
end
def self.json_create(json_str)
new(json_str["data"]["var1"])
end
attr_reader :var1
end
class Obj2
def initialize(var2)
@var2 = var2
end
def to_json(*a)
{
"json_class" => self.class,
"data" => {"var2" => @var2}
}.to_json(*a)
end
def self.json_create(json_str)
new(json_str["data"]["var2"])
end
attr_reader :var2
end
class Obj
def initialize(obj1, obj2)
@obj1 = obj1
@obj2 = obj2
end
def to_json(*a)
{
"json_class" => self.class,
"data" => {"obj1" => @obj1.to_json, "obj2" => @obj2.to_json}
}.to_json(*a)
end
def self.json_create(json_str)
new(json_str["data"]["obj1"], json_str["data"]["obj2"])
end
def to_s
"Hi, i am obj"
end
attr_reader :obj1, :obj2
end
obj1 = Obj1.new("i am obj1")
obj2 = Obj2.new("i am obj2")
obj = Obj.new(obj1,obj2)
obj_json_str = obj.to_json
puts "JSON string of obj = #{obj_json_str}"
obj_1 = JSON.parse(obj_json_str)
puts "obj_1 from json string , obj1.class = #{obj_1.obj1.class}, obj2.class = #{obj_1.obj2.class}"
上面代碼中,嵌套對象我們慣性思維,是先將對象自己轉(zhuǎn)換為JSON string:
復(fù)制代碼 代碼如下:
"data" => {"obj1" => @obj1.to_json, "obj2" => @obj2.to_json}
上面代碼輸出:
復(fù)制代碼 代碼如下:
JSON string of obj = {"json_class":"Obj","data":{"obj1":"{\"json_class\":\"Obj1\",\"data\":{\"var1\":\"i am obj1\"}}","obj2":"{\"json_class\":\"Obj2\",\"data\":{\"var2\":\"i am obj2\"}}"}}
obj_1 from json string , obj1.class = String, obj2.class = String
我們注意到,被嵌套的對象轉(zhuǎn)換為JSON string后,多了一個反斜杠 \ :
復(fù)制代碼 代碼如下:
JSON string of obj = {"json_class":"Obj","data":{"obj1":"{\"json_class\":\"Obj1\",\"data\":{\"var1\":\"i am obj1\"}}","obj2":"{\"json_class\":\"Obj2\",\"data\":{\"var2\":\"i am obj2\"}}"}}
且,JSON string轉(zhuǎn)換后,obj對象中嵌套的對象obj1和obj2,其類型都為String,而不是期望的Obj1和Obj2類型
復(fù)制代碼 代碼如下:
obj_1 from json string , obj1.class = String, obj2.class = String
實際上,這里是慣性思維害人,被嵌套的對象,不需要調(diào)用其to_json方法。
因此將Obj類的to_json代碼:
復(fù)制代碼 代碼如下:
def to_json(*a)
{
"json_class" => self.class,
"data" => {"obj1" => @obj1.to_json, "obj2" => @obj2.to_json}
}.to_json(*a)
end
修正為:
復(fù)制代碼 代碼如下:
def to_json(*a)
{
"json_class" => self.class,
"data" => {"obj1" => @obj1, "obj2" => @obj2}
}.to_json(*a)
end
然后,運行代碼,可以看到預(yù)期的輸出:
復(fù)制代碼 代碼如下:
JSON string of obj = {"json_class":"Obj","data":{"obj1":{"json_class":"Obj1","data":{"var1":"i am obj1"}},"obj2":{"json_class":"Obj2","data":{"var2":"i am obj2"}}}}
obj_1 from json string = {"json_class"=>"Obj", "data"=>{"obj1"=>#, "obj2"=>#}}
您可能感興趣的文章:- 使用Ruby來處理JSON的簡單教程
- Ruby和Ruby on Rails中解析JSON格式數(shù)據(jù)的實例教程