This is a creation in Article, where the information may have evolved or changed.
Determining Type Transformations
1234567891011121314151617181920212223242526272829303132333435 |
PackageMainImport("Encoding/json" "FMT")typeFstruct{HstringKint}typeAstruct{BstringCintDMap[string]intE []intF F} func main() {txt: =' {"B": "Hello word", "C": 1, "D": {"a": 1, "B": 2}, "E": [2,3,4,5], "F": {"H": "A", "K": 1} ' varAB A json. Unmarshal ([]byte(TXT), &ab) fmt. PRINTLN (AB. B) fmt. PRINTLN (AB. C forKey, Val: =Range(AB. D) {fmt. Println (Key, Val)} for_, E: =Range(AB. E) {fmt. Println (E)} fmt. PRINTLN (AB. F.H) fmt. PRINTLN (AB. F.K)} |
To determine the type of conversion, you need to know exactly what type of JSON document to turn into, can be nested, and can be a custom type. Compare the model docking for the document database.
Indeterminate type transformation
123456789101112131415161718192021222324252627282930313233 |
PackageMainImport("Encoding/json" "FMT") func Decode(obj map[string]interface{}) { forK, V: =Rangeobj {SwitchVV: = V. (type) { Case string: FMT. Println (k,"is string", VV) Case int: FMT. Println (k,"is int", VV) Case float64: FMT. Println (k,"is Float64", VV) Case[]Interface{}: FMT. Println (k,"is an array:") forI, U: =RangeVV {FMT. Println (I, U)}default: FMT. Println (k,"is of a type I don ' t know what to handle") } }} func main() {txt: =' {"A": 1, "B": "Hello", "C": {"a": 1, "B": 2.5}, "D": ["a", "B", "C"]} ' varAbMap[string]Interface{} json. Unmarshal ([]byte(TXT), &ab) Decode (AB)} |
The conversion of an indeterminate type, without knowing what type the JSON document will go to, can infer value by type. (type) or reflect. ValueOf (value) is used with switch, and it is not good to encode every parse-and-transform place.
Recursive model transformation
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
PackageMainImport("Encoding/json" "FMT") func jsondecode(str []byte) map[string]interface {} {varDictMap[string]Interface{} ERR: = JSON. Unmarshal (str, &dict)ifErr! =Nil{FMT. PRINTLN (ERR)return Nil}returnDict func jsonencode(dict interface{}) []byte {str, err: = json. Marshal (Dict)ifErr! =Nil{FMT. PRINTLN (ERR)return Nil}println(string(str))returnStrtypeAstruct{BstringCintD *a} func main() {varA AvarHK A A = a{B:"Hello word!"C:1, d:&a{B:"Hello ABC"C:2D:Nil,},} FMT. PRINTLN (A.B) str, _: = json. Marshal (a)println(string(str)) Json. Unmarshal (str, &HK) FMT. PRINTLN (HK). D.B)} |
Recursive type conversions allow depth to be amplified and not allow for the dynamic type of breadth.
Static language and dynamic language in the JSON processing is very obvious, dynamic language processing is too convenient, that is, the static language processing is more difficult, but actually development, should use certain things, as far as possible to avoid uncertainty, once and for all, type inference easy to use when determining the data type, Recursive model processing is good, and some specific scenarios are useful.