猿问

GoLang 中的 Marshall 和 UnMarshall JSON 内容

我有一个示例 json 文件,其结构如下


{

  "method":"brute_force",

  "bc":"select * from blah;",

  "gc":[

    "select sum(year) from blah;",

    "select count(*) from table;"

      ]

}

我正在尝试编写一个可以读取此文件并操作 json 内容的 go 程序。


package main 

import (

    "fmt"

    "encoding/json"

    "io/ioutil"

    )



type Response2 struct {

    method string

    bc string

    gc []string

}


func main() {

    file,_ := ioutil.ReadFile("config.json")

    fmt.Printf("%s",string(file))


        res := &Response2{}



        json.Unmarshal([]byte(string(file)), &res)

        fmt.Println(res)


        fmt.Println(res.method)

        fmt.Println(res.gc)


}

res.method 和 res.gc 不打印任何东西。我不知道出了什么问题。


梵蒂冈之花
浏览 295回答 1
1回答
随时随地看视频慕课网APP

相关分类

Go
我要回答