Hyperledger fabric 学习笔记: go语言 与 JSON

1、JSON 介绍
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。具体介绍可参考https://zh.wikipedia.org/wiki/JSON
2、JSON 与 Go
Go语言自带的JSON转换库为 encoding/json。
http://golang.org/doc/articles/json_and_go.html,这是官方的一篇博客,介绍的挺详细,不过是英文版的。
3、用处

//引用 hyperledger fabric-ca 的go语言代码
type RegistrationRequest struct {
    // Name is the unique name of the identity
    Name string `json:"id" help:"Unique name of the identity"`
    // Type of identity being registered (e.g. "peer, app, user")
    Type string `json:"type" help:"Type of identity being registered (e.g. 'peer, app, user')"`
    // Secret is an optional password.  If not specified,
    // a random secret is generated.  In both cases, the secret
    // is returned in the RegistrationResponse.
    Secret string `json:"secret,omitempty" help:"The enrollment secret for the identity being registered"`
    // MaxEnrollments is the maximum number of times the secret can
    // be reused to enroll.
    MaxEnrollments int `json:"max_enrollments,omitempty" help:"The maximum number of times the secret can be reused to enroll."`
    // is returned in the response.
    // The identity's affiliation.
    // For example, an affiliation of "org1.department1" associates the identity with "department1" in "org1".
    Affiliation string `json:"affiliation" help:"The identity's affiliation"`
    // Attr is used to support a single attribute provided through the fabric-ca-client CLI
    Attr string `help:"Attributes associated with this identity (e.g. hf.Revoker=true)"`
    // Attributes associated with this identity
    Attributes []Attribute `json:"attrs,omitempty"`
}

结构体成员变量 ‘xxxx’ 里面的 json字符串是Go语言的structTag。
structTag:如果希望手动配置结构体的成员和JSON字段的对应关系,可以在定义结构体的时候给成员打标签:
使用omitempty熟悉,如果该字段为nil或0值(数字0,字符串”“,空数组[]等),则打包的JSON结果不会有这个字段。
样例:点击打开链接
4、其他用法
可参考博客:http://blog.csdn.net/tiaotiaoyly/article/details/38942311

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值