【问题标题】:(un)marshalling json golang not working(un)编组json golang不起作用
【发布时间】: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

【问题讨论】:

标签: json go


【解决方案1】:

例如,

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:"test"}
encoded json = {"clip":"test2"}

游乐场:

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

导出结构字段。

type testStruct struct {
    Clip string `json:"clip"`
}

Exported identifiers

可以导出一个标识符以允许另一个标识符访问它 包裹。如果两者都导出一个标识符:

  • 标识符名称的第一个字符是 Unicode 大写字母(Unicode 类“Lu”);和
  • 标识符在包块中声明或者是字段名或方法名。

不导出所有其他标识符。

【讨论】:

  • 哇...我知道大写的东西,但从未意识到它与 json 可编码性有任何关系(这是一个词吗?)。感谢您缺少洞察力
  • 在 JSON 中的两个键相同但一个大写的边缘情况下,这将导致混乱,因为 json.Unmarshal() 总是采用最后一个 kv 对,无论它是否大写.显然这是针对jsonrpc.org/historical/…。 JSON反序列化是否有任何符合规范的函数?
  • @IsilmëO。我相信你可以做类似Clip string `json:"clip"` OtherClip string `json:"Clip"`
  • 男孩!..我遇到了这个问题,花了半天时间挠头。没有意识到不是大写是原因。
【解决方案2】:

结构字段名大写

type testStruct struct {
    clip string `json:"clip"` // Wrong.  Lowercase - other packages can't access it
}

改为:

type testStruct struct {
    Clip string `json:"clip"`
}

【讨论】:

    【解决方案3】:

    在我的例子中,我的结构字段是大写的,但我仍然遇到同样的错误。 然后我注意到我的字段的外壳不同。我的请求中必须使用下划线。

    例如: 我的请求正文是:

    {
      "method": "register",
      "userInfo": {
        "fullname": "Karan",
        "email": "email@email.com",
        "password": "random"
      }
    }
    

    但我的 golang 结构是:

    type AuthRequest struct {
        Method   string   `json:"method,omitempty"`
        UserInfo UserInfo `json:"user_info,omitempty"`
    }
    

    我通过将请求正文修改为:

    {
      "method": "register",
      "user_info": {
        "fullname": "Karan",
        "email": "email@email.com",
        "password": "random"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多