如何解析下面格式的json数组

 {

    "machines": [{

        "name": "relay_1",

        "totalmem": "3G",

        "machinemem": "6G"

    }, {

        "name": "relay_2",

        "totalmem": "30G",

        "machinemem": "4G"

    }]

}

尝试使用下面的代码进行解析


Run: func(cmd *cobra.Command, args []string) {

                fmt.Println("relay called")

                conf, _ = rootCmd.Flags().GetString("conf")

                if conf != "" {

                        fmt.Println("From Create Command : ", conf)

                }

                data, err := ioutil.ReadFile("file.txt") // data has type []byte

                if err != nil {

                        log.Fatal(err)

                }

                var result []map[string]interface{}

                json.Unmarshal(data, &result)

                relays := result["relays"].(map[string]interface{})

                for key, relay := range relays {

                        fmt.Println("name :", relay["name"],

                                "totalmem:", relay["totalmem"],

                                "relaymem:", relay["relaymem"])


                }

        },

但我收到如下错误,表明该类型无效


cmd/create_relay.go:54:29:无效类型断言:结果["relays"].(map[string])(左侧为非接口类型map[string]interface {})


有人可以让我知道如何使用下面的接口解析下面的 json


数组json字典去


白猪掌柜的
浏览 172回答 2
2回答

qq_花开花谢_0

它与下面的代码一起工作。type Relays struct {&nbsp; &nbsp; Relays []Relay `json:"relays"`}type Relay struct {&nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp;string `json:"name"`&nbsp; &nbsp; Totalmem&nbsp; &nbsp;string `json:"totalmem"`&nbsp; &nbsp; Relaymem&nbsp; &nbsp;string&nbsp; &nbsp; `json:"relaymem"`}// relayCmd represents the relay commandvar createrelayCmd = &cobra.Command{&nbsp; &nbsp; &nbsp; &nbsp; Use:&nbsp; &nbsp;"relay",&nbsp; &nbsp; &nbsp; &nbsp; Short: "A brief description of your command",&nbsp; &nbsp; &nbsp; &nbsp; Long: `A longer description that spans multiple lines and likely contains examplesand usage of using your command. For example:Cobra is a CLI library for Go that empowers applications.This application is a tool to generate the needed filesto quickly create a Cobra application.`,&nbsp; &nbsp; &nbsp; &nbsp; Run: func(cmd *cobra.Command, args []string) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("relay called")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conf, _ = rootCmd.Flags().GetString("conf")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if conf != "" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("From Create Command : ", conf)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonFile, err := os.Open(conf)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if we os.Open returns an error then handle it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Successfully Opened users.json")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // defer the closing of our jsonFile so that we can parse it later on&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer jsonFile.Close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data, err := ioutil.ReadAll(jsonFile) // data has type []byte&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var relays Relays&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.Unmarshal(data,&relays)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < len(relays.Relays); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("User Name: " + relays.Relays[i].Name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("User Totalmem: " + relays.Relays[i].Totalmem)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("User Relaymem: " + relays.Relays[i].Relaymem)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },}

幕布斯7119047

result在您的代码中是一个地图数组,您正在使用result["relays"]它是无效的。你的代码应该是这样的:Run: func(cmd *cobra.Command, args []string) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("relay called")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conf, _ = rootCmd.Flags().GetString("conf")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if conf != "" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("From Create Command : ", conf)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data, err := ioutil.ReadFile("file.txt") // data has type []byte&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result map[string]interface{}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.Unmarshal(data, &result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; relays := result["relays"].([]interface{})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _, relay := range relays {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; relayM := relay.(map[string]interface{})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("name :", relayM["name"].(string),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "totalmem:", relayM["totalmem"].(string),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "relaymem:", relayM["relaymem"].(string))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go