Shembull: Enkodimi/dekodimi i map në JSON

package main

import (
    "encoding/json"
    "fmt"
)

func main() {

    // enkodimi
    harta := map[string]int{"Hyrje": 3, "Funksionet": 11, "Shembuj": 77}
    fmt.Printf("%T\n", harta)
    fmt.Println(harta)
    fmt.Println("--------")

    jHarta, _ := json.Marshal(harta)
    fmt.Printf("%T\n", jHarta)
    fmt.Println(string(jHarta))
    fmt.Println("--------")

    // dekodimi
    b := []byte(jHarta)
    var d map[string]interface{}
    if err := json.Unmarshal(b, &d); err != nil {
        panic(err)
    }
    fmt.Printf("%T\n", d)
    fmt.Println(d)

}

https://play.golang.org/p/8SF21flSep5

Rezultati:

map[string]int
map[Funksionet:11 Hyrje:3 Shembuj:77]
\--------
[]uint8
{"Funksionet":11,"Hyrje":3,"Shembuj":77}
\--------
map[string]interface {}
map[Funksionet:11 Hyrje:3 Shembuj:77]
All Rights Reserved Theme by 404 THEME.