<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] **JSON**(JavaScript Object Notation)是一種輕量級的數據交換格式,可使人們很容易地進行閱讀和編寫,同時也方便了機器進行解析和生成。JSON適用于進行數據交互的場景,如網站前臺與后臺之間的數據交互,不同語言之間數據傳遞,數據保存到文件中等等... ## 序列化與反序列化 ```go // Student 是學生的結構體 type Student struct { Sid int Name string Age int8 } // Class 是班級的結構體 type Class struct { Title string Student []*Student } // NewStudent 是 Student 結構體的構造函數 func NewStudent(sid int, name string, age int8) *Student { return &Student{ Sid: sid, Name: name, Age: age, } } func main() { s1 := NewStudent(1, "jiaxzeng", 22) s2 := NewStudent(2, "xiaodunan", 18) c1 := Class{"Cloud computing", []*Student{s1, s2}} fmt.Printf("c1序列化前: %#v\n", c1) // json序列化 // 傳任何類型的數據,返回byte類型和error date, err := json.Marshal(c1) if err == nil { fmt.Println("c1序列化byte:", date) fmt.Println("c1序列化string:", string(date)) } else { fmt.Println("json marshal failed, err:", err) } // json反序列化 var c2 Class err = json.Unmarshal(date, &c2) if err == nil { fmt.Printf("c2反序列化: %#v\n", c2) } else { fmt.Println("json unmarshal failed, err:", err) } } // 運行結果 // c1序列化前: main.Class{Title:"Cloud computing", Student:[]*main.Student{(*main.Student)(0xc0000be000), (*main.Student)(0xc0000be020)}} // c1序列化byte: [123 34 84 105 116 108 101 34 58 34 67 108 111 117 100 32 99 111 109 112 117 116 105 110 103 34 44 34 83 116 117 100 101 110 116 34 58 91 123 34 83 105 100 34 58 49 44 34 78 97 109 101 34 58 34 106 105 97 120 122 101 110 103 34 44 34 65 103 101 34 58 50 50 125 44 123 34 83 105 100 34 58 50 44 34 78 97 109 101 34 58 34 120 105 97 111 100 117 110 97 110 34 44 34 65 103 101 34 58 49 56 125 93 125] // c1序列化string: {"Title":"Cloud computing","Student":[{"Sid":1,"Name":"jiaxzeng","Age":22},{"Sid":2,"Name":"xiaodunan","Age":18}]} // c2反序列化: main.Class{Title:"Cloud computing", Student:[]*main.Student{(*main.Student)(0xc0000be0a0), (*main.Student)(0xc0000be0e0)}} ``` >[warning] `注意` > - 結構體成員變量是小寫的話,序列化后是看不到這個字段的 > - 結構體名稱是小寫,是可以正常序列化 ## 結構體標簽(Tag) 從上面的示例可知,序列化后的變量名稱也是大寫的。需求序列化后變量名稱是小寫的。 在結構體定義上設計標簽即可,例如由下示例: ```go // Student 是學生的結構體 type Student struct { Sid int `json:"id"` Name string `json:"name"` Age int8 `json:"age"` } // Class 是班級的結構體 type Class struct { Title string Student []*Student } // NewStudent 是 Student 結構體的構造函數 func NewStudent(sid int, name string, age int8) *Student { return &Student{ Sid: sid, Name: name, Age: age, } } func main() { s1 := NewStudent(1, "jiaxzeng", 22) s2 := NewStudent(2, "xiaodunan", 18) c1 := Class{"Cloud computing", []*Student{s1, s2}} fmt.Printf("c1序列化前: %v\n", c1) // json序列化 // 傳任何類型的數據,返回byte類型和error date, err := json.Marshal(c1) if err == nil { // fmt.Println("c1序列化byte:", date) fmt.Println("c1序列化后string:", string(date)) } else { fmt.Println("json marshal failed, err:", err) } // json反序列化 var c2 Class err = json.Unmarshal(date, &c2) if err == nil { fmt.Printf("c2反序列化后: %v\n", c2) } else { fmt.Println("json unmarshal failed, err:", err) } } // 運行結果 // c1序列化前: {Cloud computing [0xc0000be000 0xc0000be020]} // c1序列化后string: {"Title":"Cloud computing","Student":[{"id":1,"name":"jiaxzeng","age":22},{"id":2,"name":"xiaodunan","age":18}]} // c2反序列化后: {Cloud computing [0xc0000be0a0 0xc0000be0e0]} ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看