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

时间:2018-01-29 10:31:29

标签: json go

我正在导入" encoding / json" 包以在 json 中打印输出。我能够获得json格式但我想修改响应结构。 这是示例代码

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)

输出以下回复

[
{
    "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": ""
    }
}
]

但我想回复以下格式

{
"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": ""
        }
    }
        ]
}

如何修改响应格式?我是Go的初学者。谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

为了更改响应的结构,您需要提供要使用类型实现的结构。

您可以定义Response结构类型,其中包含datastatus字段:

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 :(得分:0)

您只需将响应和请求对象传递给您正在使用的函数w.Header().Set("Content-Type", "application/json")。您可以附加到结构,也可以使用数组中的多个值对其进行初始化。

package main

import "encoding/json"

//  Data struct
type Data struct {
    status string
    data   []Person
}

// Person struct
type Person struct {
    Id        string
    Firstname string
    Lastname  string
    Address   Address
}

// Address struct
type Address struct {
    City  string
    State string
}

func main() {
    people := Data{
        status: "200",
        data: []Person{
            {Id: "1", Firstname: "John", Lastname: "Doe", Address: Address{City: "City X", State: "State X"}},
            {Id: "2", Firstname: "Koko", Lastname: "Doe", Address: Address{City: "City Z", State: "State Y"}},
            {Id: "3", Firstname: "Francis", Lastname: "Sunday", Address: Address{City: "City Z", State: "State Y"}},
        },
    }
    json.NewEncoder(w).Encode(people)
}