(un)编组json golang不工作 [英] (un)marshalling json golang not working

查看:120
本文介绍了(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:

http://play.golang.org/p/3XaVougMTE



导出结构字段。

 类型t​​estStruct结构{
剪辑字符串`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\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"`
}

Exported identifiers

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆