【发布时间】:2014-09-02 17:04:07
【问题描述】:
如果Error 和Success 结构中的任何一个为空,我正在尝试使它们消失
package main
import (
"encoding/json"
"net/http"
)
type appReturn struct {
Suc *Success `json:"success,omitempty"`
Err *Error `json:"error,omitempty"`
}
type Error struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
type Success struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
j := appReturn{&Success{}, &Error{}}
js, _ := json.Marshal(&j)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
输出:
{
success: { },
error: { }
}
如何从 JSON 输出中隐藏 Error 或 Success 结构?
我认为将指针作为参数发送就可以了。
【问题讨论】:
标签: json google-app-engine struct go