douzhong6480 2016-03-17 22:26
浏览 101
已采纳

将字符串数组序列化为json

So, I've been tinkering with go and have a small problem. I have something that needs to be serialised into a json like so.

{
  "name" : "Steel", 
  "things" : ["Iron", "Carbon"]
}

The struct to hold this looks like this.

type Message struct {
    name string
    things []string

}

and my code itself like this

func main() {
    i := Message{"Steel", []string{"Iron", "Carbon"}}
    fmt.Println(i);

    b, _ := json.Marshal(i)
    fmt.Printf(" Json %v
", b);

    var o Message;
    json.Unmarshal(b, &o)
    fmt.Printf(" Decoded %v
", o);
}

When I deserialise the data though, I get back an empty Message like so

{Steel [Iron Carbon]}
 Json [123 125]
 Decoded { []}

What am I doing wrong and how do I get it to work?

展开全部

  • 写回答

1条回答 默认 最新

  • dpe77294 2016-03-17 22:33
    关注

    Export fields of the struct. Unexported fields are not included by encoding/json

    type Message struct {
        Name string
        Things []string
    }
    

    The field names should begin with capital letters (Exported).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部