這是我的 JSON 帖子的示例:{ "jsonFilter" :[{ "column": "", "contains": "", "condition": "", "not": true }, { "column": "", "contains": "", "condition": "", "not": true }]}我的 Echo 框架處理程序:func GetFilteredFileData(c echo.Context) error { body := echo.Map{} if err := c.Bind(&body); err != nil { fmt.Println(err) } fmt.Println(body) for key, element := range body { fmt.Println(key, element) }}結(jié)果:jsonFilter [map[column:StartTimestamp condition:contains contains:<nil> not:false] map[column:SourceIP condition:startsWith contains:<nil> not:true]]這不是索引可訪問的,也不是鍵可訪問的。有人告訴我我需要制作一個或 2 個結(jié)構(gòu),但我在那里所做的所有努力都沒有奏效(大猩猩模式、json.Marshal 等)獲取此映射并使其在變量/結(jié)構(gòu)引用中實際可用的正確方法是什么?
1 回答

翻閱古今
TA貢獻1780條經(jīng)驗 獲得超5個贊
您可以將該 json 解組為以下結(jié)構(gòu):
type Body struct {
Filter []JSONFilter `json:"jsonFilter"`
}
type JSONFilter struct {
Column string `json:"column"`
Contains string `json:"contains"`
Condition string `json:"condition"`
Not bool `json:"not"`
}
我不知道這個回聲框架,但看起來像這樣的東西應(yīng)該可以工作:
body := Body{}
if err := c.Bind(&body); err != nil {
fmt.Println(err)
}
或者,json.Unmarshal如果以上不做。
- 1 回答
- 0 關(guān)注
- 161 瀏覽
添加回答
舉報
0/150
提交
取消