2016-01-04 66 views
0

我有一個GO功能查詢數據庫,並返回兩列:GO:格式SQL輸出爲JSON

var colA string 
var colB string 

    err = db.QueryRow("select colA, colB from table where colA = %v", 1).Scan(&colA,&colB) 
    if err != nil { 
     fmt.Printf(err.Error()) 
    } 
    fmt.Println(colA,colB) 

    return nil 

我想返回輸出JSON格式如{可樂:COLB}。我玩過json.Marshal函數,但無法正常工作。我可能失去了一些東西在這裏真的很簡單...

回答

0

你可以做一個結構,如果你知道你的數據將是:如

type User struct { 
    Username string `json:"username"` 
    Email string `json:"email"` 
} 

然後在您的查詢:

user := User{} 
err = db.QueryRow("select colA, colB from table where colA = %v", 1).Scan(&user.Username,&user.Email) 
    if err != nil { 
     fmt.Printf(err.Error()) 
} 

然後與json.Marshal包叫元帥它

msg, err := json.Marshal(user) 
if err != nil { 
    log.Println(err) 
} 

fmt.Println(string(msg)) // "{ "username": "Blah", "email": "[email protected]" }" 

此外,如果你是在struct上調用Marshal必須在字段名稱上使用大寫的首字母來導出您的字段,例如, Username

//該字段被該包忽略。字段int json:"-"

//字段以JSON形式顯示爲鍵「myName」。字段int json:"myName"

//字段以JSON形式顯示爲鍵「myName」,//如果該字段的值爲空,則該字段從對象中省略 //如上定義。現場INT json:"myName,omitempty"

//字段出現在JSON作爲關鍵的「現場」(默認值),但是//如果空 場被跳過。 //注意前導逗號。現場INT json:",omitempty"

https://golang.org/pkg/encoding/json/#Marshal

0

我會建議尋找到一些數據庫管理構架。

我個人使用gorm(github.com/jinzhu/gorm)來滿足我的數據庫需求。它具有使用結構自動創建數據庫並將其解析出來的功能。

您可以將它與「encoding/json」包配對,以將它從json傳入/傳出數據庫。

下面是一些個人的代碼,你可以將其作爲參考:

Struct

type Application struct { 
    Id   int64   `json:"id"` 
    UserID  int64   `sql:"not null;" json:"user_id"` 
    Name   string   `sql:"size:255; not null; unique;" json:"name"` 
    ExposedPorts string   `json:"exposed_ports"` //docker 
    DockerImage string   `sql:"size:255; not null;" json:"docker_image"` 
    Dependencies string   `json:"dependencies"` 
    IsEnabled  bool   `sql:"default:true" json:"is_enabled"` 

}

JSON:

func (a *Application) GetJSON() (string, error) { 
    b, err := json.Marshal(a) 
    if err != nil { 
     logging.Log(err) 
     return "",err; 
    } 
    return string(b),err; 
} 

Database

//Get application information 
func GetApplication(id int64) (*models.Application, error) { 
app := &models.Application{} 

err := db.Where(&models.Application{Id: id}).First(&app).Error 

return app, err 
} 

func GetApplications() ([]models.Application, error) { 
    //Returns a list of all applications 
    apps := []models.Application{} 

    err := db.Find(&apps).Error 

    return apps, err 
} 

//delete application from database 
func DeleteApplication(id int64) (bool, error) { 
    logging.Log("Deleting Application: ", id) 

    app := models.Application{} 

    err := db.Where(&models.Application{Id: id}).First(&app).Error 

    if err != nil { 
     return false, err 
    } 
    // TODO: Check for auth 
    //  Delete all containers 

    //Delete application from database 
    err = db.Delete(&app).Error 

    if err != nil { 
     return false, err 
    } 

    return true, err 

} 

//Update Application 
func UpdateApplication(app *models.Application) (bool, error) { 

    newapp := models.Application{} 
    err := db.Where(&models.Application{Id: app.Id}).First(&newapp).Error 

    if err != nil { 
     return false, err 
    } 

    err = db.Save(&app).Error 

    if err != nil { 
     return false, err 
    } 

    return true, nil 
} 

希望這會有所幫助:)