Internet more than 20 years, has reached the crossroads. Before the advent of the blockchain, the internet was known as the classical Internet, and the use of blockchain technology in the Internet only entered the post-Internet era. As a new technology, the blockchain is undoubtedly on the cusp, and its development prospects for the general public will eventually be positive. But at present, because the blockchain technology is in the early stage of development, there are some problems, such as technology maturity and limited application scenario, the Brother Education advises users to carefully consider and discern before choosing professional Go Language + blockchain training institution.
Today encountered an interface need to work with a JSON map type array, the idea is to use the array in the Simple-json to read arrays, and then iterate through the array to fetch each map, and then read the corresponding values, in the subsequent operation, seemingly very simple work, but encountered a trap.
The JSON format is similar to the bottom:
{"Code": 0
, "request_id": xxxx
, "code_msg": ""
, "body": [{
"device_id": "XXXX"
, "Device_hid": "XXXX"
}]
, "Count": 0}
Soon according to the above ideas to write the code, but it happened, compile, but look at the code logic there is no problem, where is the problem?
The original is interface{} The array method returned is a interface{} type, we are all in Golang interface is a universal recipient can save any type of argument, but ignore the point, it is not to take for granted when any type to use, Be sure to judge the interface type before using it. I ignored this at first, and took the interface variable for granted to cause the error.
Here's a small example.
Package Main
Import (
"Encoding/json"
"FMT"
"Github.com/bitly/go-simplejson"
)
Func Main () {
Patchwork JSON body as map array
var rbody []map[string]interface{}
T: = Make (map[string]interface{})
t["device_id"] = "DDDDDD"
t["Device_hid"] = "DDDDDDD"
Rbody = append (rbody, T)
T1: = Make (map[string]interface{})
t1["device_id"] = "AAAAA"
t1["Device_hid"] = "AAAAA"
Rbody = Append (rbody, T1)
Cnnjson: = Make (map[string]interface{})
cnnjson["code"] = 0
cnnjson["request_id"] = 123
cnnjson["code_msg"] = ""
cnnjson["Body"] = Rbody
cnnjson["page"] = 0
cnnjson["page_size"] = 0
B, _: = json. Marshal (Cnnjson)
CNNN: = string (b)
Fmt. Println ("cnnn:%s", cnnn)
Cn_json, _: = Simplejson. Newjson ([]byte (CNNN))
Cn_body, _: = Cn_json. Get ("Body"). Array ()
For _, Di: = Range Cn_body {
This is where the di is judged by type.
Newdi, _: = Di. (map[string]interface{})
device_id: = newdi["device_id"]
Device_hid: = newdi["Device_hid"]
Fmt. Println (Device_hid, device_id)
}
}
high-energy early warning, brother even education blockchain live course August continues to hit the hot!
The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!
Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!! Limited time limit!! First come first served!!
Http://www.ydma.cn/open/course/24
Follow brother Lian Blockchain technology public number get more technical dry Goods Oh!!!