小编eri*_*ric的帖子

Decode Multiple JSON Fields into one in Golang

I am working with an API that, frustratingly, has field names that vary for the same value. For example, one API response could look like this:

{
  "PersonFirstName": "John",
  "PersonLastName": "Smith"
}
Run Code Online (Sandbox Code Playgroud)

while another looks like this:

{
  "FirstNm": "John",
  "LastNm": "Smith"
}
Run Code Online (Sandbox Code Playgroud)

Suppose then I had a struct which I would like to decode my JSON to. It might look like this:

type Name struct {
    FirstName string
    LastName  string
}
Run Code Online (Sandbox Code Playgroud)

Typically I would just be able to do …

json go

5
推荐指数
1
解决办法
1539
查看次数

标签 统计

go ×1

json ×1