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
}

转载于:https://my.oschina.net/johnhjwsosd/blog/869737

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值