dousi1994 2016-02-08 14:46
浏览 31
已采纳

golang,陶醉,如何解析发布json?

I'm rather new in golang, but I'm trying hard..

I'm trying to send json by post request to the revel controller, and parse it on the revel side.

But while getting the result I cannot Unmarshal it... I'm sending an array

json_encode(array("one","two","three"))

But I can't find the correct way to work with such a data. I'm not sure do I need to make json before sending it or not..

func (c KpiCtrl) GetData() revel.Result {
    content, _ := ioutil.ReadAll(c.Request.Body)
    ...
    return c.RenderJson(content)
    }

returns

"WyJvbmUiLCJ0d28iLCJ0aHJlZSJd"

I tried to use json.Unmarshal but it returns errors.. What is the best practice to work with post data sent by curl to revel controller?

  • 写回答

1条回答 默认 最新

  • douluyezhen4512 2016-02-08 15:43
    关注

    Just use standart json Decoder:

       var content []string
       err := json.NewDecoder(c.Request.Body).Decode(&content)
       if err != nil {
           log.Fatal("JSON decode error: ", err)
       }
       defer c.Request.Body.Close()
       fmt.Println(content)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)