英文:
Wrap API Json response in Go
问题
很抱歉,我只能提供中文翻译,无法执行代码。以下是您要翻译的内容:
对不起,如果这是一个愚蠢的问题,因为我在Go语言方面非常新手。
根据业务逻辑,我调用了几个API,返回不同类型的响应,如JSON数组、嵌套JSON和单个JSON对象。
我需要将根据业务逻辑调用的API响应包装成一个通用格式,如下所示:
{
"data": "这里是API响应",
"statusCode": 200
}
我尝试了一些方法,但输出不符合预期。
type Model[T any] struct {
Data T
StatusCode int
}
model := Model[string]{Data: apiResponse, StatusCode: 200}
out, err := json.Marshal(model)
这段代码的输出是:
{
"Data": "[{\"name\":\"Harry Potter\",\"city\":\"London\"},{\"name\":\"Don Quixote\",\"city\":\"Madrid\"},{\"name\":\"Joan of Arc\",\"city\":\"Paris\"},{\"name\":\"Rosa Park\",\"city\":\"Alabama\"}]",
"StatusCode": 200
}
我进行了以下更改:
var result []map[string]interface{}
json.Unmarshal([]byte(body), &result)
out, err := json.Marshal(result)
输出结果符合预期,当使用[]map[string]interface{}
时,上述API响应是正确的JSON格式。
问题是,只有返回JSON数组的API才适用于此方法。对于返回单个JSON对象的API,为了使其正常工作,我需要进行以下更改:
map[string]interface`
即移除数组映射。
我希望能够使其通用化,以便任何类型的JSON响应都可以映射到其中。
英文:
I am sorry if this is a silly question because i am very new in Go.
I am calling couple of apis on the base of business logic, different types of response coming like json array, nest json and single json object.
i need to wrap a api response that called according to business logic in a common format like:
{
"data":"api response here",
"statusCode":200
}
i tried some but its not expect output
type Model[T any] struct {
Data T
StatusCode int
}
model := Model[string]{Data: apiResponse, StatusCode: 200}
out, err := json.Marshal(model)
out put of this code is
{
"Data": "[{\"name\":\"Harry Potter\",\"city\":\"London\"},{\"name\":\"Don Quixote\",\"city\":\"Madrid\"},{\"name\":\"Joan of Arc\",\"city\":\"Paris\"},{\"name\":\"Rosa Park\",\"city\":\"Alabama\"}]",
"StatusCode": 200
}
this i made these changes
var result []map[string]interface{}
json.Unmarshal([]byte(body), &result)
out, err := json.Marshal(result)
output was as expected, above api response was in proper json when use []map[string]interface
problem is, its only for those api that return array of json. those apis returning single json object then to make it work i need to do this
map[string]interface`
means remove the array map.
i need to make it generic so that any kind of json response map into it.
答案1
得分: 1
使用interface{}
类型的Data
字段
type APIResponse struct {
Data interface{} `json:"data"`
StatusCode int `json:"statusCode"`
}
然后你可以将任何API响应类型分配给Data
字段并进行编组。
func main() {
r := []Person{
{
Name: "Harry Porter",
City: "London",
},
{
Name: "Don Quixote",
City: "Madrid",
},
}
res := APIResponse{
Data: r,
StatusCode: 200,
}
resByt, err := json.Marshal(res)
if err != nil {
panic(err)
}
fmt.Println(string(resByt))
}
输出结果
{"data":[{"name":"Harry Porter","city":"London"},{"name":"Don Quixote","city":"Madrid"}],"statusCode":200}
在Playground中运行完整代码。
英文:
Use type of field Data
as an interface{}
type APIResponse struct {
Data interface{} `json:"data"`
StatusCode int `json:"statusCode"`
}
And then you can assign any API Response type to the Data
field and marshal it.
func main() {
r := []Person{
{
Name: "Harry Porter",
City: "London",
},
{
Name: "Don Quixote",
City: "Madrid",
},
}
res := APIResponse{
Data: r,
StatusCode: 200,
}
resByt, err := json.Marshal(res)
if err != nil {
panic(err)
}
fmt.Println(string(resByt))
}
Output
{"data":[{"name":"Harry Porter","city":"London"},{"name":"Don Quixote","city":"Madrid"}],"statusCode":200}
Run the full code here in Playground.
答案2
得分: 0
你可以简单地这样做:
result := map[string]interface{}{
"data": apiResponse,
"statusCode": 200,
}
out, err := json.Marshal(result)
英文:
You can simply do:
result:=map[string]interface{} {
"data": apiResponse,
"statusCode": 200,
}
out, err:=json.Marshal(result)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论