英文:
GoLang Gin - Binding JSON data from POST request rewrites all Identifier Fields to 0
问题
我正在使用GoLang迈出第一步,并且正在设置一个API服务器,该服务器能够从POST请求中读取JSON文件并将其保存到内存中。
我有一个以下的JSON文件:
[
{
"id": 0,
"name": "kubernetes",
"uri": "https://github.com/kubernetes/kubernetes"
},
{
"id": 1,
"name": "jenkins",
"uri": "https://github.com/jenkinsci/jenkins"
}
]
我正在将其POST到运行在本地端口上的API服务器。
这是我的setupRoutes()函数:
func setupRoutes() {
// 初始化路由器
router := gin.Default()
// 初始化路由
router.GET("/api/projects", getProjects)
router.GET("/api/projects/:id", getProjectByIdentifier)
router.POST("/api/projects", uploadProjects)
// 启动路由器
router.Run("localhost:8080")
}
这是我的uploadProjects()函数:
// 从POST请求中读取文件,并将其保存到内存中。
func uploadProjects(c *gin.Context) {
// 初始化对象
var obj []Project
// 将JSON数据绑定到对象
c.BindJSON(&obj)
fmt.Println(obj) // 用于测试:绑定的内容是什么。
// 将数据保存到内存中
proj = obj
}
这是Project结构体:
type Project struct {
Identifier int64 json:"id"
Name string json:"name"
Uri string json:"uri"
}
执行完这些代码后,我可以立即打印出数据,即绑定的内容,或者可以通过GET /api/projects调用获取数据,结果始终是:
[{0 kubernetes https://github.com/kubernetes/kubernetes} {0 jenkins https://github.com/jenkinsci/jenkins}]
我尝试过:
- 在结构体的Identifier字段中尝试了string、int和int64类型的交换。
- 进行了大量的谷歌搜索。
这可能是非常简单的问题,但是我真的不知道从哪里开始查找,所以非常感谢任何帮助。
英文:
I am taking my first steps with GoLang, and currently setting up an API Server, which is able to read JSON file from POST Request and save that to Memory.
I have a JSON File as Following:
[
{
"id": 0,
"name": "kubernetes",
"uri": "https://github.com/kubernetes/kubernetes"
},
{
"id": 1,
"name": "jenkins",
"uri": "https://github.com/jenkinsci/jenkins"
}
]
Which I am POST:ing to the API Server running on local port.
Here is my setupRoutes() - function:
func setupRoutes() {
// Initialize Router
router := gin.Default()
// Initialize Routes
router.GET("/api/projects", getProjects)
router.GET("/api/projects/:id", getProjectByIdentifier)
router.POST("/api/projects", uploadProjects)
// Start the Router
router.Run("localhost:8080")
}
and here is my uploadProjects() - function:
// Reads file from POST request, and saves that to Memory.
func uploadProjects(c *gin.Context) {
// Initialize Object
var obj []Project
// Bind JSON Data to Object
c.BindJSON(&obj)
fmt.Println(obj) // For Testing: What is binded.
// Save Data to Memory
proj = obj
}
and here is the Project Struct:
type Project struct {
Identifier int64 `json: id`
Name string `json: name`
Uri string `json: uri`
}
After executing this - I can print that data out right away, what is being binded or I can fetch that with my GET /api/projects - call, and the result is always:
[{0 kubernetes https://github.com/kubernetes/kubernetes} {0 jenkins https://github.com/jenkinsci/jenkins}]
What I've tried:
- I've tried to swap between string, int and int64 types of Identifier Field in my struct.
- Googled a Bunch
This is probably something very simple, but I don't really know where to look at this point, so any help is appreciated.
答案1
得分: 1
id
字段与字段名Identifier
不匹配。通过使用正确格式的JSON字段标签进行修复。问题中使用的字段标签未被JSON编解码器识别。
type Project struct {
Identifier int64 `json:"id"`
Name string `json:"name"`
Uri string `json:"uri"`
}
请注意,修复后的代码中的字段标签已经使用了正确的格式,即在标签名称前加上了冒号。
英文:
The id
field does not match the field name Identifier
. Fix by using properly formatted JSON field tags. The field tags used in the question are not recognized by the JSON codec.
type Project struct {
Identifier int64 `json:"id"`
Name string `json:"name"`
Uri string `json:"uri"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论