json - 是否可以绑定(bind)到自定义结构类型的 map 对象?

标签 json go data-binding go-echo

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

这是我的自定义结构类型

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){
           ... ...
 });

然后,不要绑定(bind)“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...

为什么不在 map 对象中绑定(bind)自定义结构类型?




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

像这样。

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' 结构类型的数据必须具有“Common”'Key:value' 映射...

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

最佳答案

你的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) {
    ......
});

关于json - 是否可以绑定(bind)到自定义结构类型的 map 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53405374/

相关文章:

html - 使用 knockout.js 的级联下拉列表

java - 从 Json 到 Gson 的数据模型映射返回 null

go - 并发读取文件的最佳方式

c# - DataGridView 使用 DataSourceNullValue : enter either nothing, 或 nullValue

javascript - d3.js 更新数据绑定(bind)

regex - 如何在 Golang 正则表达式中的第一个匹配项之前插入子字符串?

JSON 结构无法在转换往返中幸存

Javascript 或 Flash 导出到 CSV/Excel

json - 带有 URLSession 的 Swift 中的 SendGrid API 请求

go - 如何使用未导出结构体的函数