普通网友 2017-02-22 22:46
浏览 67
已采纳

Beego / Golang-查询要构造的值

I have an API built in Golang using the Beego framework and I have a single query that has multiple joins and then returns a JSON result.

I was hoping there is a way for me to cast each row into a struct which has nicer key names, IE rather than "Stage__Description" just "Stage", etc... So I built a ResultMap struct with the key names I want, should I be creating a Map instead?

Query:

type ResultMap struct {
    Id        int
    DateAdded time.Time
    FirstName string
    LastName  string
    Username  string
    Stage     string
    station   string
    status    string
}

//var maps []ResultMap
var maps []orm.Params
num, err := o.QueryTable("test_result_detail").
    Filter("Result__Serial", "121994-0001").
    Values(&maps, "id", "date_added", "stage__description", "station__station", "status__status", "operator__username", "operator__first_name", "operator__last_name" )

JSON RESULT

[
  {
    "DateAdded": "2016-10-20T00:00:00-05:00",
    "Id": 8306105,
    "Operator__FirstName": "Jose",
    "Operator__LastName": "Mendez",
    "Operator__Username": "3362",
    "Stage__Description": "VR1 Test",
    "Station__Station": "TS0653",
    "Status__Status": "PASS"
  },
  {
    "DateAdded": "2016-10-20T00:00:00-05:00",
    "Id": 8306465,
    "Operator__FirstName": "Jose",
    "Operator__LastName": "Mendez",
    "Operator__Username": "3362",
    "Stage__Description": "QA Lab X-Ray Inspection",
    "Station__Station": "LABEQP-0004",
    "Status__Status": "PASS"
  },
  {
    "DateAdded": "2016-10-28T00:00:00-05:00",
    "Id": 8547267,
    "Operator__FirstName": "Jose",
    "Operator__LastName": "Mendez",
    "Operator__Username": "3362",
    "Stage__Description": "Capture Customer SN",
    "Station__Station": "N/A",
    "Status__Status": "PASS"
  },
  {
    "DateAdded": "2016-10-28T00:00:00-05:00",
    "Id": 8547851,
    "Operator__FirstName": "Jose",
    "Operator__LastName": "Mendez",
    "Operator__Username": "3362",
    "Stage__Description": "Final Test",
    "Station__Station": "TS0653",
    "Status__Status": "PASS"
  },
  {
    "DateAdded": "2017-02-14T00:00:00-06:00",
    "Id": 10993864,
    "Operator__FirstName": "Jose",
    "Operator__LastName": "Mendez",
    "Operator__Username": "3362",
    "Stage__Description": "QA Mechanical Final Inspection",
    "Station__Station": "VISUAL INSPECTION",
    "Status__Status": "PASS"
  }
]
  • 写回答

1条回答 默认 最新

  • doudun5009 2017-02-23 02:49
    关注

    You want to use json annotations on your struct, then use json.Unmarshal to put the data into your struct.

    Here is an example:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        // The struct you want to store the data in
        // Note the json tags that show which json field corresponds to which struct field
        type Data struct {
            FirstName string `json:"Operator__FirstName"`
            LastName  string `json:"Operator__LastName"`
        }
        // Create a slice of these structs since our json is an array of results
        var structData []Data
    
        // The json you want to dump into the structs
        jsonData := []byte(`[{"Operator__FirstName": "Bob", "Operator__LastName": "Smith"},{"Operator__FirstName": "John", "Operator__LastName": "Adams"}]`)
    
        _ = json.Unmarshal(jsonData, &structData)
    
        fmt.Printf("%+v", structData)
    }
    

    And here it is in Go Playground: https://play.golang.org/p/Z2P7oUVT9i

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度