我正在解析大型 JSON 文件,我需要解析它的子对象items。
您可以看到它items的类型[]interface{}:
我成功解析了第一个对象map[]string]interface{},但之后我尝试解析pods["items"]类型中的哪个对象,[]interface{}然后我将它转换[]byte为json.Unmarshal函数,但它失败了:
interface {} 是 []interface [],而不是 []uint8
这是重现的代码:
package main
import (
"encoding/json"
"fmt"
)
var mytest string = `
{
"kind":"PodList",
"apiVersion":"v1",
"metadata":{
},
"items":[{
"metadata":{
"name":"super-user-pod5551",
"namespace":"kube-system",
"selfLink":"/api/v1/namespaces/kube-system/pods/super-user-pod5551",
"uid":"fe9cf599-6e82-4a1b-8db5-5e319a58e1da"
},
"spec":{
"volumes":[
{
"name":"bootstrap-signer-token-dmp5q",
"secret":{
"secretName":"bootstrap-signer-token-dmp5q",
"defaultMode":420
}
}
],
"containers":[
{
"name":"redis",
"image":"busybox:1.28",
"resources":{
},
"volumeMounts":[
{
"name":"bootstrap-signer-token-dmp5q",
"readOnly":true,
"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount"
}
],
"terminationMessagePath":"/dev/termination-log",
"terminationMessagePolicy":"File",
"imagePullPolicy":"IfNotPresent",
"securityContext":{
"capabilities":{
"add":[
"SYS_TIME"
]
}
}
}
],
"restartPolicy":"Always",
"terminationGracePeriodSeconds":30,
"schedulerName":"default-scheduler"
},
}
}
]
}
`
链接到 GoPlayground。
莫回无
当年话下
相关分类