[
{
"addition_links": {
"build_history": {
"absolute": false,
"href": "/api/v2.0/projects/test/repositories/zhangxueliang%252Fmyapp/artifacts/sha256:5f4afc8302ade316fc47c99ee1d41f8ba94dbe7e3e7747dd87215a15429b9102/additions/build_history"
}
},
"digest": "sha256:5f4afc8302ade316fc47c99ee1d41f8ba94dbe7e3e7747dd87215a15429b9102",
"extra_attrs": {
"architecture": "amd64",
"author": null,
"created": "2018-02-25T06:04:41.923389438Z",
"os": "linux"
},
"id": 176,
"labels": null,
"manifest_media_type": "application/vnd.docker.distribution.manifest.v2+json",
"media_type": "application/vnd.docker.container.image.v1+json",
"project_id": 7,
"pull_time": "2021-12-17T01:59:08.570Z",
"push_time": "2021-01-06T01:58:10.546Z",
"references": null,
"repository_id": 110,
"size": 6755615,
"tags": [
{
"artifact_id": 176,
"id": 199,
"immutable": false,
"name": "v2",
"pull_time": "2021-12-17T01:59:08.570Z",
"push_time": "2021-01-06T01:58:10.568Z",
"repository_id": 110,
"signed": false
}
],
"type": "IMAGE"
},
{
"addition_links": {
"build_history": {
"absolute": false,
"href": "/api/v2.0/projects/test/repositories/zhangxueliang%252Fmyapp/artifacts/sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e/additions/build_history"
}
},
"digest": "sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e",
"extra_attrs": {
"architecture": "amd64",
"author": "MageEdu <mage@magedu.com>",
"created": "2018-03-02T03:39:41.482586301Z",
"os": "linux"
},
"id": 175,
"labels": null,
"manifest_media_type": "application/vnd.docker.distribution.manifest.v2+json",
"media_type": "application/vnd.docker.container.image.v1+json",
"project_id": 7,
"pull_time": "2021-12-17T01:59:08.502Z",
"push_time": "2021-01-06T01:58:09.419Z",
"references": null,
"repository_id": 110,
"size": 6757253,
"tags": [
{
"artifact_id": 175,
"id": 198,
"immutable": false,
"name": "v1",
"pull_time": "2021-12-17T01:59:08.502Z",
"push_time": "2021-01-06T01:58:09.437Z",
"repository_id": 110,
"signed": false
}
],
"type": "IMAGE"
}
]
go代码
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
simplejson "github.com/bitly/go-simplejson"
)
func main() {
// 使用%252F转义"/"
url := "http://192.168.111.xxx:8603/api/v2.0/projects/test/repositories/zhangxueliang%252Fmyapp/artifacts"
resp, err := http.Get(url)
if err != nil {
fmt.Println("报错了")
return
}
body, _ := ioutil.ReadAll(resp.Body)
var rr []map[string]interface{}
json.Unmarshal(body, &rr)
for _, v := range rr {
b, _ := json.Marshal(v)
sj, err := simplejson.NewJson(b)
if err != nil {
panic(err)
}
tags, err := sj.Get("tags").Array()
fmt.Println(tags)
}
}
结果
解析出name字段
// fmt.Println(tags)
for _, value := range tags {
tagname, _ := value.(map[string]interface{})
fmt.Println(tagname["name"])
}
如果不进行断言判断,就会报错:
可参考链接:
https://studygolang.com/articles/345