dpj0015 2016-03-31 06:46
浏览 51
已采纳

如何使用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 2016-03-31 07:17
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?