我是通过beego框架,将请求过来的json进行解析,并将值保存在结构体中

--------------------1----------------------------------

 request := UpdateCommentRequestData{}
	req := common.Request{Data: request}
	err := json.Unmarshal(controller.Ctx.Input.RequestBody, &req)
---------------------------------------------
其中 UpdateCommentRequestData的结构是这样的

type UpdateCommentRequestData struct {
	Id []string `json:"id"`
}
 

common.request的结构是这样的

type Request struct {
	UserId uint64 `json:"userId,string"`
	Data interface{} `json:"data"`
}


我使用1中的代码进行解析,发现request.Id的值是空的,但是传来的json是存在Id值的,当时一头雾水,就不断在日志中打印,后来定位到是数据类型存在问题,

在1中的代码里面,Data字段传的是request的值,是值的拷贝,也就是说,json解析后的数据并不是赋值到reques中,所以使用request.Id并不会取到值,

如果将代码改成这样,再使用request.Id就可以取到值了

req := common.Request{Data: request},


GitHub 加速计划 / js / json
39
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6 binary -> binary_t Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 21 天前
f3dc4684 Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 28 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐