dpj0015
dpj0015
2016-03-31 06:46

如何使用Golang在JSON响应中创建自定义键

已采纳

I get a following json response through golang.

 [
    {
        "CreatedOn": "03-22-2015",
        "JSONReceived": [
            {
                "EzpOrderID": "ezp_126",
                "FirstName": "Kumar",
                "LastName": "S",
                "OrderDesc": "Sample"
            }
        ],
        "Status": "0",
        "id": "80acbdad-8aae-4d6c-ac63-2a02a9db64b4"
    },
    {
        "CreatedOn": "03-22-2015",
        "JSONReceived": [
            {
                "EzpOrderID": "ezp_126",
                "FirstName": "Vasanth",
                "LastName": "K",
                "OrderDesc": "Sample"
            }
        ],
        "Status": "0",
        "id": "8f7f52a5-793a-45bd-a9b7-ed41495e0ee3"
    }
]..

But i need to create with key in response. sample response as follows. How to achieve using golang programming.

 {
    "returnResponseData": [{
        "CreatedOn": "03-22-2015",
        "JSONReceived": [{
            "EzpOrderID": "ezp_126",
            "FirstName": "Kumar",
            "LastName": "S",
            "OrderDesc": "Sample"
        }],
        "Status": "0",
        "id": "80acbdad-8aae-4d6c-ac63-2a02a9db64b4"
    }, {
        "CreatedOn": "03-22-2015",
        "JSONReceived": [{
            "EzpOrderID": "ezp_126",
            "FirstName": "Vasanth",
            "LastName": "K",
            "OrderDesc": "Sample"
        }],
        "Status": "0",
        "id": "8f7f52a5-793a-45bd-a9b7-ed41495e0ee3"
    }]
}

Please help me to achieve this task using golang.

Whole Source code as follows:

 func orderList(w http.ResponseWriter, request *http.Request) {

    rows, err := r.Table("orders").Run(session)
    if err != nil {
        fmt.Println(err)
        return
    }

    var resultSet []interface{}

    err = rows.All(&resultSet)
    if err != nil {
        fmt.Printf("Error scanning database result: %s", err)
        return
    }

    if origin := request.Header.Get("Origin"); origin != "" {
        w.Header().Set("Access-Control-Allow-Origin", origin)
        w.Header().Set("Access-Control-Allow-Methods", "GET")
        w.Header().Set("Access-Control-Allow-Headers",
        "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
    }


    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(200)            
    json.NewEncoder(w).Encode(resultSet)



}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douqin2108 douqin2108 5年前

    You have to define another type like this:

    type Wrapper struct {
        ReturnResponseData []interface{} `json:"returnResponseData"`
    }
    

    and then encode wrapper, containing your response set:

    json.NewEncoder(w).Encode(&Wrapper{ReturnResponseData: resultSet})
    

    Notice, that you have to use property tag to achieve the name "returnResponseData", starting of a small letter (because encoder doesn't encode private properties by default).

    点赞 评论 复制链接分享

相关推荐