解码json
// 这个示例程序展示如何解码 JSON 字符串package mainimport ( "
// 这个示例程序展示如何解码 JSON 字符串 package main import ( "encoding/json" "fmt" "log" "os" ) // JSON 包含要反序列化的样例字符串 var JSON = `{ "name": "Gopher", "title": "programmer", "contact": { "home": 77, "cell": "415.555.5555" }, "color":[ "red", "blue" ] }` func main() { // 将 JSON 字符串反序列化到 map 变量 var c map[string]interface{} err := json.Unmarshal([]byte(JSON), &c) if err != nil { log.Println("ERROR:", err) return } fmt.Println("Name:", c["name"]) fmt.Println("Title:", c["title"]) fmt.Println("Contact") fmt.Print("H:", c["contact"].(map[string]interface{})["home"]) fmt.Println("C:", c["contact"].(map[string]interface{})["cell"]) create, err := os.Create("aa.txt") color,err2:=c["color"].([]interface{}) fmt.Println(color,err2) if err2{ for _,curColor:=range color{ //进行类型断言 curStr:=curColor.(string) create.Write([]byte(curStr+"\r\n")) } } }
相关文章
发表评论
评论列表
- 这篇文章还没有收到评论,赶紧来抢沙发吧~