无法将[] interface {}转换为[] byte:“ interface {}是[] interface [],而不是[] uint8”

E235:

我正在解析大型JSON文件,我需要解析其子对象items
您可以看到items类型为[]interface{}
在此处输入图片说明

我成功解析了第一个对象,map[]string]interface{}但是在此之后,我尝试解析pods["items"]类型正确的对象[]interface{},并将其转换[]bytejson.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"
         },
         "status":{
            "phase":"Failed",
            "message":"The node was low on resource: ephemeral-storage. ",
            "reason":"Evicted",
            "startTime":"2020-05-10T11:13:33Z"
         }
      }
    ]
}
`


func main(){

    //var pods interface{}
    var pods map[string]interface{}
    err := json.Unmarshal([]byte(mytest), &pods)
    fmt.Println(err)
    //var spec map[string]interface{}
    //err = json.Unmarshal(byte(pods["items"]), &pods["items"])
    var items []interface{}

    err = json.Unmarshal(pods["items"].([]byte), &items)
    fmt.Println(err)

}

链接到GoPlayground

史蒂文·埃克霍夫(Steven Eckhoff):

问题是您没有按照声明进行类型转换。相反,您正在执行类型声明。

既然pods["items"]是a,[]interface{}您首先需要每个接口都具有具体的接口类型,[]byte并对每个接口进行声明,然后将它们收集到中,[]byte然后才能执行解组。

最终,您应该创建一个struct可以简单地解组的,这将大大简化您的代码。

正如其他人所说的那样,您需要解组两次的事实应该表明存在更好的方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章