从JSON字符串中检索数组元素的最佳方法是什么?

huangapple go评论68阅读模式
英文:

Whats the best way to retrieve value of array element from a JSON string

问题

尝试在Go程序中从以下JSON中检索值"default-token-k99mq"...

const input = `{
  "kind": "ServiceAccount",
  "apiVersion": "v1",
  "metadata": {
    "name": "default",
    "namespace": "mynamespace",
    "selfLink": "/api/v1/namespaces/mynamespace/serviceaccounts/default",
    "uid": "483d1043-4d68-11e7-be08-3a3f3b149220",
    "resourceVersion": "425039",
    "creationTimestamp": "2017-06-09T23:06:34Z"
  },
  "secrets": [
    {
      "name": "default-token-k99mq"
    }
  ]
}`

需要从`secrets`数组中获取名称
英文:

Trying to retrieve value "default-token-k99mq" from below JSON in go program ...

const input = `{
  "kind": "ServiceAccount",
  "apiVersion": "v1",
  "metadata": {
    "name": "default",
    "namespace": "mynamespace",
    "selfLink":         "/api/v1/namespaces/mynamespace/serviceaccounts/default",
    "uid": "483d1043-4d68-11e7-be08-3a3f3b149220",
    "resourceVersion": "425039",
    "creationTimestamp": "2017-06-09T23:06:34Z"
  },
  "secrets": [
    {
      "name": "default-token-k99mq"
    }
  ]
}`

Need to get names from secrets array

答案1

得分: 1

你可以使用以下代码进行操作:

package main

import (
	"fmt"
	"encoding/json"
)

func main() {
	const input = `{
		"kind": "ServiceAccount",
		"apiVersion": "v1",
		"metadata": {
			"name": "default",
			"namespace": "mynamespace",
			"selfLink": "/api/v1/namespaces/mynamespace/serviceaccounts/default",
			"uid": "483d1043-4d68-11e7-be08-3a3f3b149220",
			"resourceVersion": "425039",
			"creationTimestamp": "2017-06-09T23:06:34Z"
		},
		"secrets": [
			{
				"name": "default-token-k99mq"
			}
		]
	}`
	type NameStruct struct {
		Name string `json:"name"`
	}
	type Secret struct {
		Secrets []NameStruct `json:"secrets"`
	}

	secret := Secret{}
	json.Unmarshal([]byte(input), &secret)
	fmt.Println(secret.Secrets[0].Name)
}

这段代码可以将JSON字符串解析为结构体,并打印出secrets数组中的第一个元素的name字段值。

英文:

You can do this https://play.golang.org/p/27eKFmBCHY

package main

import (
	"fmt"
	"encoding/json"
	
)

func main() {
	const input = `{
        "kind": "ServiceAccount",
        "apiVersion": "v1",
        "metadata": {
            "name": "default",
            "namespace": "mynamespace",
            "selfLink":      "/api/v1/namespaces/mynamespace/serviceaccounts/default",
            "uid": "483d1043-4d68-11e7-be08-3a3f3b149220",
            "resourceVersion": "425039",
            "creationTimestamp": "2017-06-09T23:06:34Z"
        },
        "secrets": [
            {
                "name": "default-token-k99mq"
            }
         ]
    }`
    type NameStruct struct {
	    Name string `json:"name"`
	}
    type Secret struct {
	    Secrets []NameStruct `json:"secrets"`
	}
	
	secret := Secret{}
	json.Unmarshal([]byte(input), &secret)
	fmt.Println(secret.Secrets[0].Name)
}

huangapple
  • 本文由 发表于 2017年6月18日 00:34:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/44606910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定