dqnk57224 2018-01-29 10:31
浏览 103
已采纳

如何在Go中设置自定义json响应结构

I am importing "encoding/json" package to print output in json.I was able to get the json format but i want to modify the response structure. Here is the sample code

var people []Person
people = append(people, Person{Id: "1", Firstname: "John", Lastname: "Doe", Address: Address{City: "City X", State: "State X"}})
people = append(people, Person{Id: "2", Firstname: "Koko", Lastname: "Doe", Address: Address{City: "City Z", State: "State Y"}})
people = append(people, Person{Id: "3", Firstname: "Francis", Lastname: "Sunday"})
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(people)

which outputs the following response

[
{
    "id": "1",
    "firstname": "John",
    "lastname": "Doe",
    "address": {
        "city": "City X",
        "state": "State X"
    }
},
{
    "id": "2",
    "firstname": "Koko",
    "lastname": "Doe",
    "address": {
        "city": "City Z",
        "state": "State Y"
    }
},
{
    "id": "3",
    "firstname": "Francis",
    "lastname": "Sunday",
    "address": {
        "city": "",
        "state": ""
    }
}
]

But i want to response into the following format

{
"status": "200",
"data": [
    {
        "id": "1",
        "firstname": "John",
        "lastname": "Doe",
        "address": {
            "city": "City X",
            "state": "State X"
        }
    },
    {
        "id": "2",
        "firstname": "Koko",
        "lastname": "Doe",
        "address": {
            "city": "City Z",
            "state": "State Y"
        }
    },
    {
        "id": "3",
        "firstname": "Francis",
        "lastname": "Sunday",
        "address": {
            "city": "",
            "state": ""
        }
    }
        ]
}

How can I modify the response format ? I am beginner in Go. Thanks for your help.

  • 写回答

2条回答 默认 最新

  • duandou2763 2018-01-29 10:50
    关注

    In order to change structure of your response, you need to provide the structure that you want to achieve with type.

    You can define Response struct type which will consist of data and status fields:

    type Response struct {
        Status string `json:"status"`
        Data []Person `json:"data"`
    }
    
    var people []Person
    people = append(people, Person{Id: "1", Firstname: "John", Lastname: "Doe", Address: Address{City: "City X", State: "State X"}})
    people = append(people, Person{Id: "2", Firstname: "Koko", Lastname: "Doe", Address: Address{City: "City Z", State: "State Y"}})
    people = append(people, Person{Id: "3", Firstname: "Francis", Lastname: "Sunday"})
    w.Header().Set("Content-Type", "application/json")
    
    resp := Response{Status: "200", Data: people}
    json.NewEncoder(w).Encode(resp)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大