【发布时间】:2014-10-25 01:09:57
【问题描述】:
我正在玩 Go,我很困惑为什么 json 编码和解码对我不起作用
我想我几乎是逐字复制了这些示例,但输出显示 marshal 和 unmarshal 都没有返回数据。他们也不会出错。
谁能提示我哪里出错了?
我的示例代码:Go playground
package main
import "fmt"
import "encoding/json"
type testStruct struct {
clip string `json:"clip"`
}
func main() {
//unmarshal test
var testJson = "{\"clip\":\"test\"}"
var t testStruct
var jsonData = []byte(testJson)
err := json.Unmarshal(jsonData, &t)
if err != nil {
fmt.Printf("There was an error decoding the json. err = %s", err)
return
}
fmt.Printf("contents of decoded json is: %#v\r\n", t)
//marshal test
t.clip = "test2"
data, err := json.Marshal(&t)
if err != nil {
fmt.Printf("There was an error encoding the json. err = %s", err)
return
}
fmt.Printf("encoded json = %s\r\n", string(data))
}
输出:
contents of decoded json is: main.testStruct{clip:""}
encoded json = {}
在两个输出中我都希望看到解码或编码的 json
【问题讨论】: