golang string to json

package main
import (
     "fmt"
     "encoding/json"
)
const json_data string = "{\"DW\": \"My\"}"
const json_datas string = "[{\"DW\": \"my\"}, {\"DW\": \"\"}]"
func main() {
     var dat map[string]interface{}
     var dats []map[string]interface{}
     fmt.Println("Hello World!", json_data)
     if err := json.Unmarshal([]byte(json_data), &dat); err == nil {
         for k, v := range dat {
             fmt.Println(k, v)
         }
     } else {
         fmt.Println(err)
     }
     if err := json.Unmarshal([]byte(json_datas), &dats); err == nil {
         for k, v := range dats {
             fmt.Println(k, v)
         }
     } else {
         fmt.Println(err)
     }
     fmt.Println(dat["DW"])
     fmt.Println(dats[0]["DW"])
}
Hello World! {"DW": "mine"}
DW my
0 map[DW:mine]
1 map[dw:of]
mine
mine

Leave a Reply

Your email address will not be published.

en_USEnglish