1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package main
import ( "encoding/json" "fmt" )
type Test1 struct { Va1 int Va2 int }
type Test2 struct { va1 int va2 int }
type Test3 struct { Va1 int `json:"te1"` Va2 int `json:"va1"` }
func main() { var test1 Test1 err := json.Unmarshal([]byte(`{"Va1":1,"Va2":2}`), &test1) if err != nil { fmt.Println(err) return } fmt.Printf("test1: %+v\n", test1)
var test11 Test1 err = json.Unmarshal([]byte(`{"va1":1,"vA2":2}`), &test11) if err != nil { fmt.Println(err) return } fmt.Printf("test11: %+v\n", test11)
var test12 Test1 err = json.Unmarshal([]byte(`{"va1":1,"vA2":2,"Va2":3}`), &test12) if err != nil { fmt.Println(err) return } fmt.Printf("test13: %+v\n", test12)
var test2 Test2 err = json.Unmarshal([]byte(`{"va1":1,"va2":2}`), &test2) if err != nil { fmt.Println(err) return } fmt.Printf("test2: %+v\n", test2)
var test3 Test3 err = json.Unmarshal([]byte(`{"va1":1,"te1":2}`), &test3) if err != nil { fmt.Println(err) return } fmt.Printf("test3: %+v\n", test3)
var test31 Test3 err = json.Unmarshal([]byte(`{"te1":2,"va1":1,"va2":3}`), &test31) if err != nil { fmt.Println(err) return } fmt.Printf("test31: %+v\n", test31) }
|