Golang Json.Marshal错误

Rah*_*sad 3 json marshalling go

我一直在尝试将地图编码为JSON,但到目前为止我还没有成功.Json.Marshal不是编码值,它只是编码密钥.

https://gist.github.com/rahulpache/9174490

package main 
import (
    "encoding/json"
    "fmt"
)
type node struct {
    value   string
    expiry  float64
    settime float64
}

func main() {
    var x = make(map[string]node)

    x["hello"] = node{value: "world", expiry: 1, settime: 2}
    x["foo"] = node{value: "bar", expiry: 1, settime: 2}

    a, err := json.Marshal(x)
    fmt.Println(string(a))
}
Run Code Online (Sandbox Code Playgroud)

输出:

{"foo":{},"hello":{}}
Run Code Online (Sandbox Code Playgroud)

ymg*_*ymg 9

您的属性和类型名称是私有的,如果您希望您的属性是公开的,您需要遵循大写每个单词的约定,例如(值而不是值)将其公之于众,将您的类型切换为此类型并且应该序列化就好了.

type Node struct {
    Value   string
    Expiry  float64
    Settime float64
}
Run Code Online (Sandbox Code Playgroud)