日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

go解析复杂json数组字符串:结合使用json和simple-json库

發(fā)布時(shí)間:2025/1/21 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go解析复杂json数组字符串:结合使用json和simple-json库 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原始json數(shù)組字符串

[{"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 mainimport ("encoding/json""fmt""io/ioutil""net/http""time"simplejson "github.com/bitly/go-simplejson" )func main() {// 使用%252F轉(zhuǎn)義"/"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("報(bào)錯(cuò)了")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)} }

結(jié)果

解析出name字段

// fmt.Println(tags)for _, value := range tags {tagname, _ := value.(map[string]interface{})fmt.Println(tagname["name"])}


如果不進(jìn)行斷言判斷,就會(huì)報(bào)錯(cuò):

可參考鏈接:
https://studygolang.com/articles/345

總結(jié)

以上是生活随笔為你收集整理的go解析复杂json数组字符串:结合使用json和simple-json库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。