【问题标题】:Is it possible to bind to map object of custom struct type?是否可以绑定到自定义结构类型的映射对象?
【发布时间】:2018-11-21 16:47:39
【问题描述】:

我的问题是, 如何在地图对象(变量)中绑定(自动绑定?)自定义结构类型?

这是我的自定义结构类型

type Tetris struct {
    ... ...
    NowBlock           map[string]int     `form:"nowBlock" json:"nowBlock"`
    ... ...
}

这是我的 ajax 代码

 $.ajax({
     type : "POST"
     , url : "/game/tetris/api/control"
     , data : {
                "keyCode" : keyCode
                , "ctxWidth" : ctxWidth
                , "ctxHeight" : ctxHeight
                , "nowBlock" : {"O":0}
     } // also, i did JSON.stringify, but did not binding..
     , dataType : "json"
     , contentType : "application/json"
     }).done(function(data){
           ... ...
 });

然后,不要绑定“NowBlock”

tetris := new(Tetris)
if err := c.Bind(tetris); err != nil {
    c.Logger().Error(err)
}
fmt.Println(tetris.NowBlock)

println 结果是,

'map[]' //nil...

这是我的完整问题链接(GOLANG > How to bind ajax json data to custom struct type?)

请帮帮我。



附言。谢谢你回答我。 我确实喜欢这个答案。 但是,它也不起作用。

首先,

- No 'contentType : "application/json"'
- don't use JSON.stringify

 then, in go side, 
- fmt.println(tetris.KeyCode) // OK
- fmt.println(tetris.NowBlock) // NOT OK.. 'map[]'

第二,

- Use 'contentType : "application/json"'
- Use JSON.stringify

then, in go side, 
- fmt.println(tetris.KeyCode) // NOT OK.. '' (nil)
- fmt.println(tetris.NowBlock) // NOT OK.. 'map[]'

第三,

i remove the custom struct type Tetris NowBlock object's `form:nowBlock` literal, 
but is does not working too...

为什么不在地图对象中绑定自定义结构类型?




我很抱歉。我解决了这个问题。 问题是我的自定义结构类型有另一个自定义结构类型。

像这样。

type Tetris struct {
    Common Common

    NowBlock           map[string]int     `json:"nowBlock"`
}

type Common struct {
    CtxWidth  int `json:"ctxWidth"`
    CtxHeight int `json:"ctxHeight"`

    KeyCode int `form:"keyCode" json:"keyCode"`
}

在这种情况下,我做到了

 $.ajax({
 type : "POST"
 , url : "/game/tetris/api/control"
 , data : {
            "keyCode" : keyCode
            , "ctxWidth" : ctxWidth
            , "ctxHeight" : ctxHeight
            , "nowBlock" : {"O":0}
 } // also, i did JSON.stringify, but did not binding..
 , dataType : "json"
 , contentType : "application/json"
 }).done(function(data){
       ... ...

});

但是,这是错误的! 正确的是,

$.ajax({
    type : "POST"
    , url : "/game/tetris/api/control"
    , data : JSON.stringify({
        "Common" : {
            "keyCode" : keyCode
            , "ctxWidth" : ctxWidth
            , "ctxHeight" : ctxHeight
        }
        , "nowBlock" : {"O":0}
    })
    , dataType : "json"
    , contentType : "application/json"
}).done(function(data){
   ... ...

在json数据中,'Common' struct type的数据必须有“Common” 'Key:value' map...

我很高兴收到您的回答和关注。

【问题讨论】:

标签: json go data-binding go-echo


【解决方案1】:

你的 go 代码没有问题。为什么 echo .Bind() 无法检索 AJAX 发送的有效载荷是因为有效载荷不是 JSON 格式。

$.ajax你需要将JSON.stringify()的数据转成JSON字符串格式。

JSON.stringify({
    "keyCode" : keyCode
    , "ctxWidth" : ctxWidth
    , "ctxHeight" : ctxHeight
    , "nowBlock" : {"O":0}
})

contentType 设置为application/json 不会自动将有效负载转换为JSON 字符串。这就是为什么仍然需要JSON.stringy()


全部更改:

var payload = JSON.stringify({
    "keyCode": keyCode,
    "ctxWidth": ctxWidth,
    "ctxHeight": ctxHeight,
    "nowBlock": {
        "O": 0
    }
})

$.ajax({
    type: "POST",
    url: "/game/tetris/api/control",
    data: payload,
    dataType: "json",
    contentType: "application/json"
}).done(function(data) {
    ......
});

【讨论】:

  • 谢谢。首先,我在 ajax 中删除了 contentType,GO tetris.KeyCode > working // 打印 'key code: 13..' 但是,tetris.NowBlock > 不工作.. // pring 'map[]' 第二个,contentType: "application/json" and data: stringify({json..}) GO tetris.KeyCode > 不工作 // 打印 nil 但是,tetris.NowBlock > 不工作.. // pring 'map[]'
  • 如果你删除contentType,payload将会以application/x-www-form-urlencoded的内容类型发送。一些数组信息将不可读
  • 抱歉我的评论不正确。所以我编辑了我的问题(请阅读问题的结尾)。
【解决方案2】:

也许你应该删除结构标签'form',当你使用'application/json'发送数据时,'form'标签是未使用的。
当我只添加'json'标签时程序运行良好,如果我添加'form'标签,echo使用'form'并得到错误。

希望对你有帮助。

【讨论】:

  • @gdk 如果你用content-type=application/json发送js数据?,你可以用这种方式检查。 1.如果数据发送正确,ioutil.ReadAll(c.Request().Body),这应该是你发送的json数据。 2. 你有正确的结构来绑定到 json 数据。 3. 使用正确的结构标签。通过 struct tag 和 content-type 回显绑定数据,application/json 的 tag json,application/x-www-form-urlencoded 的 tag 形式,url 中参数的 tag 查询。有我的测试代码:goplay.space/#A70IHubaQIH
  • 非常感谢您。我改变了我的ajax代码。添加的代码是“应用程序/json”。我的问题结束了,我的错误是自定义结构类型的对象其他自定义结构类型。你真是太好了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 2023-01-04
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多