本文介绍了(un)编组json golang不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我和Go一起玩,难以理解为什么json编码和解码不适合我
我想我几乎逐字拷贝了这些例子,但输出说,编组和解组都不返回数据。他们也不会给出错误。
任何人都可以提示我哪里出错了吗?
我的示例代码:
<$ p
$ import
$ import
$ b $ json:clip`
}
func main(){
// unmarshal test
var testJson ={\clip \ :\test \}
var t testStruct
var jsonData = [] byte(testJson)
err:= json.Unmarshal(jsonData,& t)
如果err!= nil {
fmt.Printf(解码json时出错了,err =%s,err)
return
}
fmt.Printf( 解码的json的内容是:%#v \ r \ n,t)
//编组测试
t.clip =test2
data,err := json.Marshal(& t)
if err!= nil {
fmt.Printf(编码json时出错。err =%s,err)
return
}
fmt.Printf(encoded json =%s\r\\\
,string(data))
}
输出:
解码的json的内容是:main.testStruct {clip:}
编码的json = {}
在这两个输出中,我预计会看到解码或编码的json
例如 ,
package main
importfmt
importencoding / json
type testStruct struct {
剪辑字符串`json:剪辑`
}
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(解码json时出错。 err =%s,err)
return
}
fmt.Printf(解码的json的内容是:%#v \r\\\
,t)
// marshal test
t.Clip =test2
data,err:= json.Marshal(& t)
if err!= nil {
fmt.Printf(编码json的错误。err =%s,err)
return
}
fmt.Printf(encoded json =%s\r\ n,字符串(数据))
}
输出:
解码的json的内容是:main.testStruct {Clip:test}
编码的json = {clip:test2}
Playground:
导出结构字段。 类型testStruct结构{
剪辑字符串`json:clip`
}
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\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))
}
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\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))
}
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"`
}
这篇关于(un)编组json golang不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!