Go转json数组
2017-03-29 00:00
148 查看
摘要: Golang json数组 struct
由于返回结果是一个json数组,单纯使用json.Unmarshal没法实现,于是在网上找了一下解决方案,并修改如下:
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 }
相关文章推荐
- GO语言发送json串数组完整代码
- go操作json,注意json的嵌套、数组
- 在CakePHP中利用Javascript Helper把PHP数组转换为JSON
- jquery筛选数组之grep、each、inArray、map的用法及遍历json对象
- js遍历json数组
- TreeLoader扩展,支持josn-plugin返回的json对象中包含的数组值
- js 中 字符串转换为json数组
- js 取出json数组中的值
- jquery下json数组的操作实现代码
- jquery下json数组的操作用法实例
- 为JS数组/Json/Xml添加折半查询方法
- JQuery 将json 转化成xml 格式的数组
- 关于JSON的一个小小例子,数组的调用,和获值
- Java对集合的遍历 List ArryList HashMap LinkedMap JSON 数组等
- jQuery 遍历json数组的实现代码
- js遍历json数组
- 向mvc controller传递json数组
- 关于PHP在数组对象与JSON数据之间的转换问题(编码问题)
- jquery下json数组的操作用法实例