[GO]json解析到結構體


package main

import (
    "encoding/json"
    "fmt"
)

type IT struct {
    Company string `json:"company"`
    Subjects []string `json:"subjects"`
    Isok bool `json:"isok"`
    Price float64 `json:"price"`
}

func main() {
    jsonbuf := `{"company":"zyg","isok":true,"price":5.55,"subjects":["go","python","java"]}`
    var tmp IT
    err := json.Unmarshal([]byte(jsonbuf), &tmp) //這個地方一下是取tmp的地址的,它需要對結構體進行更改,根據查看源碼可以知道它的返回就一個err
    if err != nil {
        return
    }
    fmt.Printf("tmp = %+v\n", tmp)
}

執行的結果為

tmp = {Company:zyg Subjects:[go python java] Isok:true Price:5.55}  //這個結果沒有問題,打印就是沒有雙引號的

如果其中只想需打印結果體的下面兩行,只需要修改結構體為

type IT struct {
    //Company string `json:"company"`
    //Subjects []string `json:"subjects"`
    Isok bool `json:"isok"`
    Price float64 `json:"price"`
}

那么執行的結果自動的解析 為

tmp = {Isok:true Price:5.55}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM