英文:
How to marshal struct when some members are protected/inner/hidden
问题
如何确保在编组时包含此LookupCode结构中的字段?
package main
import (
"encoding/json"
"fmt"
)
type LookupCode struct {
Code string `json:"code"`
Name string `json:"name"`
}
func (l *LookupCode) GetCode() string {
return l.Code
}
func main() {
c := &LookupCode{
Code: "A",
Name: "Apple",
}
b, _ := json.MarshalIndent(c, "", "\t")
fmt.Println(string(b))
}
你可以使用json标签来指定在编组时包含结构中的字段。在LookupCode结构中,code字段和name字段都有一个json标签,用于指定它们在编组时的名称。在这个例子中,code字段的标签是json:"code",name字段的标签是json:"name"。这样,在调用json.MarshalIndent函数时,这些字段将被包含在编组的结果中。
英文:
How do ensure the fields in this LookupCode struct are included when marshalling?
package main
import (
"encoding/json"
"fmt"
)
type LookupCode struct {
code string `json:"code"`
name string `json:"name"`
}
func (l *LookupCode) GetCode() string {
return l.code
}
func main() {
c := &LookupCode{
code: "A",
name: "Apple",
}
b, _ := json.MarshalIndent(c, "", "\t")
fmt.Println(string(b))
}
答案1
得分: 7
你可以通过实现json.Marshaller接口来实现:
完整示例:http://play.golang.org/p/8mIcPwX92P
// 实现json.Unmarshaller
func (l *LookupCode) UnmarshalJSON(b []byte) error {
var tmp struct {
Code string json:"code"
Name string json:"name"
}
err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}
l.code = tmp.Code
l.name = tmp.Name
return nil
}
func (l *LookupCode) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Code string json:"code"
Name string json:"name"
}{
Code: l.code,
Name: l.name,
})
}
英文:
You can by implementing the json.Marshaller interface:
Full Example: http://play.golang.org/p/8mIcPwX92P
// Implement json.Unmarshaller
func (l *LookupCode) UnmarshalJSON(b []byte) error {
var tmp struct {
Code string `json:"code"`
Name string `json:"name"`
}
err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}
l.code = tmp.Code
l.name = tmp.Name
return nil
}
func (l *LookupCode) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Code string `json:"code"`
Name string `json:"name"`
}{
Code: l.code,
Name: l.name,
})
}
答案2
得分: 5
encode/json无法编组未导出的字段。将您的代码更改为:
type LookupCode struct {
Code string `json:"code"`
Name string `json:"name"`
}
并在使用code或name的任何地方执行相同的操作。
Playground: http://play.golang.org/p/rak0nVCNGI
编辑
这个限制是由于在编组结构体时使用了反射。如果您需要保持值未导出,您必须实现json.Marshaller接口并手动进行编码。
英文:
encode/json cannot marshal unexported fields. Change your code to:
type LookupCode struct {
Code string `json:"code"`
Name string `json:"name"`
}
and do the same wherever you use code or name.
Playground: http://play.golang.org/p/rak0nVCNGI
Edit
The limitation is due to the reflection used when marshalling the struct. If you need to keep your values unexported, you must implement the json.Marshaller interface and do the encoding manually.
答案3
得分: 0
如果结构体只有字符串类型的字段,你可以尝试这种"hack"的方式。
package main
import (
"fmt"
"reflect"
"github.com/bitly/go-simplejson"
)
type A struct {
name string `json:"name"`
code string `json:"code"`
}
func marshal(a A) ([]byte, error) {
j := simplejson.New()
va := reflect.ValueOf(&a)
vt := va.Elem()
types := reflect.TypeOf(a)
for i := 0; i < vt.NumField(); i++ {
j.Set(types.Field(i).Tag.Get("json"), fmt.Sprintf("%v", reflect.Indirect(va).Field(i)))
}
return j.MarshalJSON()
}
func main() {
a := A{name: "jessonchan", code: "abc"}
b, _ := marshal(a)
fmt.Println(string(b))
}
这段代码是一个示例,用于将结构体A转换为JSON字符串。它使用了reflect包来获取结构体字段的信息,并使用github.com/bitly/go-simplejson包来生成JSON字符串。在marshal函数中,通过遍历结构体字段并使用反射获取字段的值,然后将字段名和值设置到simplejson对象中。最后,调用MarshalJSON方法将simplejson对象转换为JSON字符串。在main函数中,创建了一个结构体A的实例,并调用marshal函数将其转换为JSON字符串,然后打印输出。
英文:
if the struct has only string-type fields,you can try this hack way.
package main
import (
"fmt"
"reflect"
"github.com/bitly/go-simplejson"
)
type A struct {
name string `json:"name"`
code string `json:"code"`
}
func marshal(a A) ([]byte, error) {
j := simplejson.New()
va := reflect.ValueOf(&a)
vt := va.Elem()
types := reflect.TypeOf(a)
for i := 0; i < vt.NumField(); i++ {
j.Set(types.Field(i).Tag.Get("json"), fmt.Sprintf("%v", reflect.Indirect(va).Field(i)))
}
return j.MarshalJSON()
}
func main() {
a := A{name: "jessonchan", code: "abc"}
b, _ := marshal(a)
fmt.Println(string(b))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论