go interface 转 string,解析json
package main
import (
"encoding/json"
"fmt"
)
var user map[string]interface{}
func main() {
userJson := "{\"username\":\"system\",\"password\":\"123456\"}"
json.Unmarshal([]byte(userJson), &user)
fmt.Println(user) //打印结果:map[password:123456 username:system]
username := user["username"]
fmt.Println("username ==", username) //username == system
//判断username的长度不为0,
//if len(username) != 0 {//直接使用,报错invalid argument username (type interface {}) for len
if len(username.(string)) != 0 {//通过.(string)转换成string类型
fmt.Println("输入合法")
} else {
fmt.Println("输入不合法")
}
}