go與json

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   sp   檔案   資料   div   

Go語言對JSON進行編碼和解碼

http://outofmemory.cn/code-snippet/3766/Go-language-to-JSON-to-coding-jiema

package mainimport (    "fmt"    "encoding/json")func main() {    // json encode    j1 := make(map[string]interface{})    j1["name"] = "outofmemory"    j1["url"] = "http://outofmemory.cn/"    js1, err := json.Marshal(j1)    if err != nil {        panic(err)    }    println(string(js1))    // json decode    j2 := make(map[string]interface{})    err = json.Unmarshal(js1, &j2)    if err != nil {        panic(err)    }    fmt.Printf("%#v\n", j2)}
Go語言從檔案中讀取Json資料

http://www.oschina.net/code/snippet_197499_22659 

{    "13918098715":"63120523",    "13918098719":"73314985",    "13918098718":"82752863",    "13918098714":"92440329",        "13918094183":"35875678",    "13918094187":"45562314",    "13918094186":"54292576",    "13918094185":"63906263",    "13918094189":"73460548",    "13918094364":"45717870"}package main import (    "encoding/json"    "fmt"    "io/ioutil") var xxx = map[string]string{} func readFile(filename string) (map[string]string, error) {    bytes, err := ioutil.ReadFile(filename)    if err != nil {        fmt.Println("ReadFile: ", err.Error())        return nil, err    }     if err := json.Unmarshal(bytes, &xxx); err != nil {        fmt.Println("Unmarshal: ", err.Error())        return nil, err    }     return xxx, nil} func main() {    xxxMap, err := readFile("xxx.json")    if err != nil {        fmt.Println("readFile: ", err.Error())        return nil, err    }     fmt.Println(xxxMap)}

 

 

aa

go與json

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.