gpt4 book ai didi

json - 如何从JSON解析值。我正在尝试列出单个值,例如python中的字典

转载 作者:行者123 更新时间:2023-12-01 22:41:28 24 4
gpt4 key购买 nike

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

func main() {
resp, err := http.Get("https://s3.us-west-2.amazonaws.com/cloudformation-templates-us-west-2/AutoScalingMultiAZWithNotifications.template")

if err !=nil {
fmt.Println(err)

}

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
fmt.Println(err)
}

v := make(map[string] interface{})

json.Unmarshal(body, &v)

fmt.Println(v)
}

最佳答案

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

func main() {
resp, err := http.Get("https://s3.us-west-2.amazonaws.com/cloudformation-templates-us-west-2/AutoScalingMultiAZWithNotifications.template")
if err != nil {
panic(err)
}
defer resp.Body.Close()

v := make(map[string]interface{})
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
panic(err)
}

// index into map v and type-assert the value
if Parameters, ok := v["Parameters"].(map[string]interface{}); ok {
// index into Parameters and if found, print
if VpcId, ok := Parameters["VpcId"]; ok {
fmt.Println(VpcId)
}
}

}

关于json - 如何从JSON解析值。我正在尝试列出单个值,例如python中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62520155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com