Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. )
  7.  
  8. type Foo struct {
  9. Number int `json:"number"`
  10. Title string `json:"title"`
  11. }
  12.  
  13. func main() {
  14. datas := make(map[int]Foo)
  15.  
  16. for i := 0; i < 10; i++ {
  17. datas[i] = Foo{Number: 1, Title: "test"}
  18. }
  19.  
  20. jsonString, _ := json.Marshal(datas)
  21.  
  22. fmt.Println(datas)
  23. fmt.Println(jsonString)
  24. }
  25.  
  26. map[9:{1 test} 2:{1 test} 7:{1 test} 3:{1 test} 4:{1 test} 5:{1 test} 6:{1 test} 8:{1 test} 0:{1 test} 1:{1 test}]
  27.  
  28. []
  29.  
  30. jsonString, err := json.Marshal(datas)
  31. fmt.Println(err)
  32.  
  33. // [] json: unsupported type: map[int]main.Foo
  34.  
  35. datas := make(map[string]Foo, N)
  36.  
  37. for i := 0; i < 10; i++ {
  38. datas[fmt.Sprint(i)] = Foo{Number: 1, Title: "test"}
  39. }
  40. j, err := json.Marshal(datas)
  41. fmt.Println(string(j), err)
  42.  
  43. datas2 := make([]Foo, N)
  44. for i := 0; i < 10; i++ {
  45. datas2[i] = Foo{Number: 1, Title: "test"}
  46. }
  47. j, err = json.Marshal(datas2)
  48. fmt.Println(string(j), err)
  49.  
  50. package main
  51.  
  52. import (
  53. "encoding/json"
  54. "fmt"
  55. "strconv"
  56. )
  57.  
  58. // Num wraps the int value so that we can implement the TextMarshaler and TextUnmarshaler
  59. type Num int
  60.  
  61. func (n *Num) UnmarshalText(text []byte) error {
  62. i, err := strconv.Atoi(string(text))
  63. if err != nil {
  64. return err
  65. }
  66. *n = Num(i)
  67. return nil
  68. }
  69.  
  70. func (n Num) MarshalText() (text []byte, err error) {
  71. return []byte(strconv.Itoa(int(n))), nil
  72. }
  73.  
  74. type Foo struct {
  75. Number Num `json:"number"`
  76. Title string `json:"title"`
  77. }
  78.  
  79. func main() {
  80. datas := make(map[Num]Foo)
  81.  
  82. for i := 0; i < 10; i++ {
  83. datas[Num(i)] = Foo{Number: 1, Title: "test"}
  84. }
  85.  
  86. jsonString, err := json.Marshal(datas)
  87. if err != nil {
  88. panic(err)
  89. }
  90.  
  91. fmt.Println(datas)
  92. fmt.Println(jsonString)
  93.  
  94. m := make(map[Num]Foo)
  95. err = json.Unmarshal(jsonString, &m)
  96. if err != nil {
  97. panic(err)
  98. }
  99.  
  100. fmt.Println(m)
  101. }
  102.  
  103. map[1:{1 test} 2:{1 test} 4:{1 test} 7:{1 test} 8:{1 test} 9:{1 test} 0:{1 test} 3:{1 test} 5:{1 test} 6:{1 test}]
  104. [123 34 48 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 49 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 50 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 51 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 52 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 53 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 54 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 55 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 56 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 57 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 125]
  105. map[4:{1 test} 5:{1 test} 6:{1 test} 7:{1 test} 0:{1 test} 2:{1 test} 3:{1 test} 1:{1 test} 8:{1 test} 9:{1 test}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement