go - 我想获得任何结构字段值,我该怎么做?

标签 go

API 返回天气数据如下:

{
  "HeWeather6": [
    {
      "basic": {
        "cid": "CN101010100",
        "location": "北京",
        "parent_city": "北京",
        "admin_area": "北京",
        "cnty": "中国",
        "lat": "39.90498734",
        "lon": "116.4052887",
        "tz": "+8.00"
      },
      "update": {
        "loc": "2019-11-17 20:09",
        "utc": "2019-11-17 12:09"
      },
      "status": "ok",
      "now": {
        "cloud": "91",
        "cond_code": "101",
        "cond_txt": "多云",
        "fl": "-5",
        "hum": "12",
        "pcpn": "0.0",
        "pres": "1024",
        "tmp": "4",
        "vis": "8",
        "wind_deg": "300",
        "wind_dir": "西北风",
        "wind_sc": "5",
        "wind_spd": "38"
      }
    }
  ]
}

我创建了诸如
// Root - 123
type Root struct {
    HEWEATHER6 HeWeather `json:"HeWeather6"`
}

// HeWeather - 123
type HeWeather []struct {
    BASIC  Basic  `json:"basic"`
    UPDATE Update `json:"update"`
    NOW    Now    `json:"now"`
}

// Basic - 123
type Basic struct {
    Cid        string `json:"cid"`
    Location   string `json:"location"`
    ParentCity string `json:"parent_city"`
    AdminArea  string `json:"admin_area"`
    Cnty       string `json:"cnty"`
    Lat        string `json:"lat"`
    Lon        string `json:"lon"`
    Tz         string `json:"tz"`
}

// Update - 123
type Update struct {
    Loc string `json:"loc"`
    Utc string `json:"utc"`
}

// Now - 123
type Now struct {
    Cloud    string `json:"cloud"`
    CondCode string `json:"cond_code"`
    CondTxt  string `json:"cond_txt"`
    Fl       string `json:"fl"`
    Hum      string `json:"hum"`
    Pcpn     string `json:"pcpn"`
    Pres     string `json:"pres"`
    Tmp      string `json:"tmp"`
    Vis      string `json:"vis"`
    WindDeg  string `json:"wind_deg"`
    WindDir  string `json:"wind_dir"`
    WindSc   string `json:"wind_sc"`
    WindSpd  string `json:"wind_spd"`
}

例如,如何将单个位置数据存储到变量中并在标准输出上打印?

最佳答案

您可以使用 json.Unmarshal 将您的 json 解码到您的结构中,并使用点符号语法访问您的值。

package main

import (
    "encoding/json"
    "fmt"
)

// Root - 123
type Root struct {
    HEWEATHER6 HeWeather `json:"HeWeather6"`
}

// HeWeather - 123
type HeWeather []struct {
    BASIC  Basic  `json:"basic"`
    UPDATE Update `json:"update"`
    NOW    Now    `json:"now"`
}

// Basic - 123
type Basic struct {
    Cid        string `json:"cid"`
    Location   string `json:"location"`
    ParentCity string `json:"parent_city"`
    AdminArea  string `json:"admin_area"`
    Cnty       string `json:"cnty"`
    Lat        string `json:"lat"`
    Lon        string `json:"lon"`
    Tz         string `json:"tz"`
}

// Update - 123
type Update struct {
    Loc string `json:"loc"`
    Utc string `json:"utc"`
}

// Now - 123
type Now struct {
    Cloud    string `json:"cloud"`
    CondCode string `json:"cond_code"`
    CondTxt  string `json:"cond_txt"`
    Fl       string `json:"fl"`
    Hum      string `json:"hum"`
    Pcpn     string `json:"pcpn"`
    Pres     string `json:"pres"`
    Tmp      string `json:"tmp"`
    Vis      string `json:"vis"`
    WindDeg  string `json:"wind_deg"`
    WindDir  string `json:"wind_dir"`
    WindSc   string `json:"wind_sc"`
    WindSpd  string `json:"wind_spd"`
}

func main() {
    b := `{
  "HeWeather6": [
    {
      "basic": {
        "cid": "CN101010100",
        "location": "北京",
        "parent_city": "北京",
        "admin_area": "北京",
        "cnty": "中国",
        "lat": "39.90498734",
        "lon": "116.4052887",
        "tz": "+8.00"
      },
      "update": {
        "loc": "2019-11-17 20:09",
        "utc": "2019-11-17 12:09"
      },
      "status": "ok",
      "now": {
        "cloud": "91",
        "cond_code": "101",
        "cond_txt": "多云",
        "fl": "-5",
        "hum": "12",
        "pcpn": "0.0",
        "pres": "1024",
        "tmp": "4",
        "vis": "8",
        "wind_deg": "300",
        "wind_dir": "西北风",
        "wind_sc": "5",
        "wind_spd": "38"
      }
    }
  ]
}`

    t := &Root{}
    err := json.Unmarshal([]byte(b), &t)
    if err != nil {
        panic(err)
    }

    fmt.Println(t.HEWEATHER6[0].BASIC.Location)

}

这里的工作示例:https://play.golang.org/p/6iGVQeZU1_Z

关于go - 我想获得任何结构字段值,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58900488/

相关文章:

go - 名称未注册接口(interface)

pointers - Golang 结构问题指向父结构方法

postgresql - 单元测试 Postgres 数据库连接 golang

ssl - Python 的 create_default_context() 等价于 Go?

go - 在 LoggingHandlers 中自定义日志格式 在 GO 中的 Gorilla 处理程序中实现

tcp - 在 golang 中,如何使用 net.TCPConn 在不事先指定字节 slice 长度的情况下进行读取?

go - 如何使用 Go 获取客户端 DNS IP

amazon-web-services - 通过 Go SDK (Amazon S3) 从 Bucket 生成 Torrent

map - 将 map[interface {}]interface {} 转换为 map[string]string

go - 接口(interface)和类型的问题