Because of its simple and convenient data structure, JSON has gradually become the data format of the mainstream data interchange on the Internet.
Before discussing the JSON conversion method for nested objects (Nested object), let's look at a simple ruby JSON transformation.
First, the Ruby object is converted to a JSON string:
Copy Code code as follows:
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}"
As we can see in the code above, there are three key points to the conversion between Ruby and JSON string:
# Introduction of the JSON library to have the following two methods, JSON is in the way of open class, the hash object with the To_json (*a) method, about Ruby Open class reference support open The opening and closing principle in the programming language of class characteristics (open-closed principle)
1) require ' JSON '
#定义对象转为JSON String's To_json (*a) method, which implements the To_json (*a) method that uses a hash object
2 def to_json (*a)
# defines the Json_create method of constructing an object from a JSON string, which is a class method
3 def self.json_create (JSON_STR)
The above three points are the basic requirements for implementing the JSON string conversion in Ruby.
The results of the code run are:
Copy Code code as follows:
JSON string of obj1 = {"Json_class": "Obj1", "data": {"var1": "I Am Obj1"}}
Ob1 from JSON string = I am obj1
Now let's look at the JSON string conversions of nested objects:
Copy Code code as follows:
#!/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}"
In the code above, the nested object we think of as inertia, is to first convert the object to a JSON string:
Copy Code code as follows:
"Data" => {"Obj1" => @obj1. To_json, "Obj2" => @obj2. To_json}
The above code output:
Copy Code code as follows:
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
We noticed that after the nested object was converted to a JSON string, there was one more backslash \:
Copy Code code as follows:
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\ "}}"}}
Also, after JSON string conversion, the nested objects in the Obj object Obj1 and Obj2, their types are string, not the expected Obj1 and OBJ2 types
Copy Code code as follows:
Obj_1 from JSON string, Obj1.class = string, Obj2.class = string
In fact, here is the inertial thinking that harms the nested object and does not need to invoke its To_json method.
So the To_json code for the obj class is:
Copy Code code as follows:
def To_json (*a)
{
"Json_class" => Self.class,
"Data" => {"Obj1" => @obj1. To_json, "Obj2" => @obj2. To_json}
}.to_json (*a)
End
Amended to:
Copy Code code as follows:
def To_json (*a)
{
"Json_class" => Self.class,
"Data" => {"obj1" => @obj1, "Obj2" => @obj2}
}.to_json (*a)
End
Then, run the code and you can see the expected output:
Copy Code code as follows:
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" =>#}}