Go轉json數組


Go轉json數組

最近因需要要調用gitlab的API,其中有一個是根據私有token獲取Repositories列表 由於返回結果是一個json數組,單純使用json.Unmarshal沒法實現,於是在網上找了一下解決方案,並修改如下:

type JSONObj struct { data interface{} } //Json Initialize the json configruation func Json(data string) *JSONObj { j := new(JSONObj) var f interface{} err := json.Unmarshal([]byte(data), &f) if err != nil { return j } j.data = f return j } //GetGitLabModel ... func (j *JSONObj) GetGitLabModel() []*models.Gitlab { modelMap := make([]*models.Gitlab, 0) for k, _ := range (j.data).([]interface{}) { model := &models.Gitlab{} if m, ok := (j.data).([]interface{}); ok { v := m[k].(map[string]interface{}) if h, ok := v["name_with_namespace"]; ok { model.Name_with_namespace = h.(string) } if h, ok := v["http_url_to_repo"]; ok { model.Http_url_to_repo = h.(string) } if h, ok := v["public"]; ok { model.Public = h.(bool) } } modelMap = append(modelMap, model) } return modelMap }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM