Di golang untuk mengubah suatu tipe data golang ke JSON dapat menggunakan fungsi Marshal, sementara untuk mengubah JSON ke suatu tipe data golang dapat menggunakan fungsi Unmarshal. Keduanya bisa digunakan untuk mengubah nilai dengan tipe data int, float?, string, bool, slice, struct, map dan interface ? menjadi JSON dan sebaliknya.
Copy func Unmarshal(data []byte, v interface{}) error
Copy func Marshal(v interface{}) ([]byte, error)
Contoh code marshal (int-> JSON) & unmarshal (JSON -> int)
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
jsonData, _ := json. Marshal ( 123 )
fmt. Println ( string (jsonData))
}
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
var jsonString = `2`
var jsonData = [] byte (jsonString)
var data int
var err = json. Unmarshal (jsonData, & data)
if err != nil {
fmt. Println (err. Error ())
return
}
fmt. Println (data)
}
Contoh code marshal (string-> JSON) & unmarshal (JSON -> string)
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
jsonData, _ := json. Marshal ( "test" )
fmt. Println ( string (jsonData))
}
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
var jsonString = `"test"`
var jsonData = [] byte (jsonString)
var data string
var err = json. Unmarshal (jsonData, & data)
if err != nil {
fmt. Println (err. Error ())
return
}
fmt. Println (data)
}
Contoh code marshal (boolean-> JSON) & unmarshal (JSON -> boolean)
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
jsonData, _ := json. Marshal ( true )
fmt. Println ( string (jsonData))
}
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
var jsonString = `true`
var jsonData = [] byte (jsonString)
var data bool
// json -> struct
var err = json. Unmarshal (jsonData, & data)
if err != nil {
fmt. Println (err. Error ())
return
}
fmt. Println (data)
}
Contoh code marshal (slice -> JSON) & unmarshal (JSON -> slice)
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
jsonData, _ := json. Marshal ([] string { "test 1" , "test 2" , "test 3" })
fmt. Println ( string (jsonData))
}
Copy ["test 1","test 2","test 3"] // -> JSON
Copy package main
import (
"encoding/json"
"fmt"
)
func main () {
var jsonString = `["test 1", "test 2", "test 3"]`
var jsonData = [] byte (jsonString)
var data [] string
// json -> struct
var err = json. Unmarshal (jsonData, & data)
if err != nil {
fmt. Println (err. Error ())
return
}
fmt. Println (data)
}
Copy [test 1 test 2 test 3]