我想将 JSON 格式化为 Golang 未编组的输出。我在那里看不到任何东西

问题描述 投票:0回答:2

我想将 JSON 格式化为 Golang 未编组的输出。我在那里看不到任何东西。

// Create an HTTP client with the API key.
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
    log.Fatal("Error creating request:", err)
}
req.Header.Add("Authorization", "Bearer "+apiKey)

// Make the HTTP request to fetch JSON data.
resp, err := client.Do(req)
if err != nil {
    log.Fatal("Error making HTTP request:", err)
}
defer resp.Body.Close()

// Read the JSON response into a byte slice.
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
    log.Fatal("Error reading JSON data:", err)
}

// Unmarshal the JSON data into a map.
var target map[string]interface{}
err = json.Unmarshal(data, &target)
if err != nil {
    log.Fatalf("Unable to unmarshal JSON due to %s", err)
}

我做了这个并得到了,

map[droplets:[map[backup_ids:[1.4132617e+08 1.41783892e+08 1.42227987e+08 1.42667107e+08 1.43107709e+08]created_at:2020-06-04T17:40:05Z磁盘:50个功能: [备份监控 private_networking ipv6] id:1.94861944e+08 image:map[created_at:2020-05-15T05:47:50Z 描述:Ubuntu 20.04 x86 发行版:Ubuntu id:6.366398e+07 min_disk_size:20 名称:20.04 (LTS) x64 public :假区域:[] size_gigabytes:2.36 slug:状态:退休标签:[]类型:base]内核:锁定:假内存:4096名称:staging-02网络:map [v4:[map [网关:10.104.0.1 ip_address :10.104.0.2 网络掩码:255.255.240.0 类型:私有] 映射[网关:128.199.64.1 ip_address:128.199.111.88 网络掩码:255.255.192.0 类型:公共]] v6:[映射[网关:2400:6180:0:d0: :1 ip_address:2400:6180:0:d0::e6d:6001 网络掩码:64 类型:public]]] next_backup_window:map[结束:2023-11-02T20:00:00Z 开始:2023-11-02T16:00: 00Z] 区域:地图[可用:真实功能:[备份 ipv6 元数据 install_agent 存储 image_transfer] 名称:新加坡 1 大小:[s-1vcpu-512mb-10gb s-1vcpu-1gb s-1vcpu-1gb-amd s-1vcpu-1gb -英特尔 s-1vcpu-1gb-35gb-英特尔 s-1vcpu-2gb s-1vcpu-2gb-amd s-1vcpu-2gb-英特尔 s-1vcpu-2gb-70gb-英特尔 s-2vcpu-2gb s-2vcpu-2gb- AMD s-2vcpu-2gb-英特尔 s-2vcpu-2gb-90gb-英特尔 s-2vcpu-4gb s-2vcpu-4gb-amd s-2vcpu-4gb-英特尔 s-2vcpu-4gb-120gb-英特尔 c-2 c2- 2vcpu-4gb s-4vcpu-8gb s-4vcpu-8gb-amd s-4vcpu-8gb-英特尔 g-2vcpu-8gb s-4vcpu-8gb-240gb-英特尔 gd-2vcpu-8gb m-2vcpu-16gb c-4 c2 -4vcpu-8gb s-8vcpu-16gb m3-2vcpu-16gb s-8vcpu-16gb-amd s-8vcpu-16gb-英特尔 g-4vcpu-16gb s-8vcpu-16gb-480gb-英特尔 so-2vcpu-16gb m6-2vcpu -16gb gd-4vcpu-16gb so1_5-2vcpu-16gb m-4vcpu-32gb c-8 c2-8vcpu-16gb m3-4vcpu-32gb g-8vcpu-32gb so-4vcpu-32gb m6-4vc....... ..

我希望这个格式更加格式化,如何做到这一点。我想提取其中的变量,然后进行一些计算,但这看起来很混乱。我无法使用这个如何格式化它?

// Create an HTTP client with the API key.
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
    log.Fatal("Error creating request:", err)
}
req.Header.Add("Authorization", "Bearer "+apiKey)

// Make the HTTP request to fetch JSON data.
resp, err := client.Do(req)
if err != nil {
    log.Fatal("Error making HTTP request:", err)
}
defer resp.Body.Close()

// Read the JSON response into a byte slice.
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
    log.Fatal("Error reading JSON data:", err)
}

// Unmarshal the JSON data into a map.
var target map[string]interface{}
err = json.Unmarshal(data, &target)
if err != nil {
    log.Fatalf("Unable to unmarshal JSON due to %s", err)
}

我做了这个并得到了,

map[droplets:[map[backup_ids:[1.4132617e+08 1.41783892e+08 1.42227987e+08 1.42667107e+08 1.43107709e+08]created_at:2020-06-04T17:40:05Z磁盘:50个功能: [备份监控 private_networking ipv6] id:1.94861944e+08 image:map[created_at:2020-05-15T05:47:50Z 描述:Ubuntu 20.04 x86 发行版:Ubuntu id:6.366398e+07 min_disk_size:20 名称:20.04 (LTS) x64 public :假区域:[] size_gigabytes:2.36 slug:状态:退休标签:[]类型:base]内核:锁定:假内存:4096名称:staging-02网络:map [v4:[map [网关:10.104.0.1 ip_address :10.104.0.2 网络掩码:255.255.240.0 类型:私有] 映射[网关:128.199.64.1 ip_address:128.199.111.88 网络掩码:255.255.192.0 类型:公共]] v6:[映射[网关:2400:6180:0:d0: :1 ip_address:2400:6180:0:d0::e6d:6001 网络掩码:64 类型:public]]] next_backup_window:map[结束:2023-11-02T20:00:00Z 开始:2023-11-02T16:00: 00Z] 区域:地图[可用:真实功能:[备份 ipv6 元数据 install_agent 存储 image_transfer] 名称:新加坡 1 大小:[s-1vcpu-512mb-10gb s-1vcpu-1gb s-1vcpu-1gb-amd s-1vcpu-1gb -英特尔 s-1vcpu-1gb-35gb-英特尔 s-1vcpu-2gb s-1vcpu-2gb-amd s-1vcpu-2gb-英特尔 s-1vcpu-2gb-70gb-英特尔 s-2vcpu-2gb s-2vcpu-2gb- AMD s-2vcpu-2gb-英特尔 s-2vcpu-2gb-90gb-英特尔 s-2vcpu-4gb s-2vcpu-4gb-amd s-2vcpu-4gb-英特尔 s-2vcpu-4gb-120gb-英特尔 c-2 c2- 2vcpu-4gb s-4vcpu-8gb s-4vcpu-8gb-amd s-4vcpu-8gb-英特尔 g-2vcpu-8gb s-4vcpu-8gb-240gb-英特尔 gd-2vcpu-8gb m-2vcpu-16gb c-4 c2 -4vcpu-8gb s-8vcpu-16gb m3-2vcpu-16gb s-8vcpu-16gb-amd s-8vcpu-16gb-英特尔 g-4vcpu-16gb s-8vcpu-16gb-480gb-英特尔 so-2vcpu-16gb m6-2vcpu -16gb gd-4vcpu-16gb so1_5-2vcpu-16gb m-4vcpu-32gb c-8 c2-8vcpu-16gb m3-4vcpu-32gb g-8vcpu-32gb so-4vcpu-32gb m6-4vc....... ..

我希望这个格式更加格式化,如何做到这一点。我想提取其中的变量,然后进行一些计算,但这看起来很混乱。我无法使用这个如何格式化它?

json api go data-structures maps
2个回答
0
投票

为了使 JSON 数据更具可读性和结构化,您可以使用 json.MarshalIndent 函数将数据封送回格式化的 JSON 字符串。具体方法如下:

// Create an HTTP client with the API key.
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
    log.Fatal("Error creating request:", err)
}
req.Header.Add("Authorization", "Bearer "+apiKey)

// Make the HTTP request to fetch JSON data.
resp, err := client.Do(req)
if err != nil {
    log.Fatal("Error making HTTP request:", err)
}
defer resp.Body.Close()

// Read the JSON response into a byte slice.
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
    log.Fatal("Error reading JSON data:", err)
}

// Unmarshal the JSON data into a map.
var target map[string]interface{}
err = json.Unmarshal(data, &target)
if err != nil {
    log.Fatalf("Unable to unmarshal JSON due to %s", err)
}

// Marshal the map back into a formatted JSON string.
formattedJSON, err := json.MarshalIndent(target, "", "  ")
if err != nil {
    log.Fatal("Error formatting JSON:", err)
}

// Now you have the formatted JSON in the 'formattedJSON' variable.
fmt.Println(string(formattedJSON))

0
投票

您可以编写一个实用函数,可以精美地打印任何给定的结构/映射/切片:

func JSONStringify(obj interface{}) {
    beautifulJsonByte, err := json.MarshalIndent(obj, "", "  ")
    body := ""
    if err != nil {
        body = fmt.Sprintf("%v", obj)
    } else {
        body = string(beautifulJsonByte)
    }
    
    fmt.Println(string(beautifulJsonByte))
}

用途:

var target map[string]interface{}

err = json.Unmarshal(data, &target)
if err != nil {
    log.Fatalf("Unable to unmarshal JSON due to %s", err)
}

JSONStringify(target)
© www.soinside.com 2019 - 2024. All rights reserved.