Shembull: Strukt në JSON, JSON në strukt

package main

import (
    "encoding/json"
    "fmt"
)

type Anetari struct {
    Emri   string `json:"user_name"`
    Emaili string `json:"user_email"`
    Admin  bool   `json:"is_admin"`
}

func main() {
    a := Anetari{
        Emri:   "Arben",
        Emaili: "arben@gmail.com",
        Admin:  true,
    }

    // Nga struct ne JSON

    b, _ := json.Marshal(a)
    fmt.Println(string(b))

    // Nga JSON ne struct

    var r Anetari
    json.Unmarshal(b, &r)
    fmt.Println("Emri: ", r.Emri)
    fmt.Println("Emaili: ", r.Emaili)
    fmt.Println("Admin: ", r.Admin)
}

https://play.golang.org/p/6J19r3TT7KP

Rezultati:

{"user_name":"Arben","user_email":"arben@gmail.com","is_admin":true}
Emri:  Arben
Emaili:  arben@gmail.com
Admin:  true
All Rights Reserved Theme by 404 THEME.