dragon88112 2016-09-30 05:18
浏览 36
已采纳

无论如何,不​​声明结构就将编码的结构作为JSON发送?

I would like to know how I could simplify this piece of code:

type PP struct {
    Profile_picture string `json:"profile_picture"`
}

json.NewEncoder(w).Encode(PP {result.Profile_picture})

Something like:

json.NewEncoder(w).Encode({result.Profile_picture})

^ this give me: syntax error: missing operand

And to get rid of:

type PP struct {
    Profile_picture string `json:"profile_picture"`
}

Thanks. Sorry for my english.

  • 写回答

2条回答 默认 最新

  • doude5860 2016-09-30 05:22
    关注
    json.NewEncoder(w).Encode(
        map[string]string{"profile_picture": result.Profile_picture},
    )
    

    would work — maps with string keys encode to JSON as objects, and you can build up whatever you like with them. It's not shorter, but it does avoid the helper type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真