(un) 编组 json golang 不起作用 [英] (un)marshalling json golang not working
问题描述
我正在玩 Go 并且不明白为什么 json 编码和解码对我不起作用
我想我几乎是逐字逐句地复制了示例,但输出显示 marshal 和 unmarshal 都没有返回数据.他们也不会出错.
谁能指出我哪里出错了?
我的示例代码:去游乐场
包主导入fmt"导入编码/json"类型 testStruct 结构 {剪辑字符串`json:剪辑"`}功能主(){//解组测试var testJson = "{"clip":"test"}"var t 测试结构var jsonData = []byte(testJson)错误 := json.Unmarshal(jsonData, &t)如果错误!= nil {fmt.Printf("解码json时出错.err = %s", err)返回}fmt.Printf("解码后的json内容为:%#v
", t)//元组测试t.clip = "test2"数据,错误:= json.Marshal(&t)如果错误!= nil {fmt.Printf("json 编码出错.err = %s", err)返回}fmt.Printf("编码的json = %s
", string(data))}
输出:
解码后的json内容为:main.testStruct{clip:""}编码的 json = {}
在两个输出中我都希望看到解码或编码的 json
例如
包主导入fmt"导入编码/json"类型 testStruct 结构 {剪辑字符串`json:"clip"`}功能主(){//解组测试var testJson = "{"clip":"test"}"var t 测试结构var jsonData = []byte(testJson)错误 := json.Unmarshal(jsonData, &t)如果错误!= nil {fmt.Printf("解码json时出错.err = %s", err)返回}fmt.Printf("解码后的json内容为:%#v
", t)//元组测试t.Clip = "test2"数据,错误:= json.Marshal(&t)如果错误!= nil {fmt.Printf("json 编码出错.err = %s", err)返回}fmt.Printf("编码的json = %s
", string(data))}
输出:
解码后的json内容为:main.testStruct{Clip:"test"}编码的 json = {"clip":"test2"}
游乐场:
http://play.golang.org/p/3XaVougMTE
导出结构字段.
type testStruct struct {剪辑字符串`json:"clip"`}
<块引用>
可以导出标识符以允许从另一个标识符访问它包裹.如果两者都导出标识符:
- 标识符名称的第一个字符是Unicode大写字母(Unicode类Lu");和
- 标识符是在包块中声明的,或者是字段名或方法名.
不会导出所有其他标识符.
I'm playing with Go and am stumped as to why json encode and decode don't work for me
I think i copied the examples almost verbatim, but the output says both marshal and unmarshal return no data. They also don't give an error.
can anyone hint to where i'm going wrong?
my sample code: 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
", 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
", string(data))
}
output:
contents of decoded json is: main.testStruct{clip:""}
encoded json = {}
in both outputs I would have expected to see the decoded or encoded json
For example,
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
", 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
", string(data))
}
Output:
contents of decoded json is: main.testStruct{Clip:"test"}
encoded json = {"clip":"test2"}
Playground:
http://play.golang.org/p/3XaVougMTE
Export the struct fields.
type testStruct struct {
Clip string `json:"clip"`
}
An identifier may be exported to permit access to it from another package. An identifier is exported if both:
- the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
- the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.
这篇关于(un) 编组 json golang 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!