Golang如何解组嵌套的JSON数据的子集

Posted 技术社区

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang如何解组嵌套的JSON数据的子集相关的知识,希望对你有一定的参考价值。

{
    "coord": {
        "lon": -0.13,
        "lat": 51.51
    },
    "weather": [
        {
            "id": 300,
            "main": "Drizzle",
            "description": "light intensity drizzle",
            "icon": "09d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 280.32,
        "pressure": 1012,
        "humidity": 81,
        "temp_min": 279.15,
        "temp_max": 281.15
    },
    "visibility": 10000,
    "wind": {
        "speed": 4.1,
        "deg": 80
    },
    "clouds": {
        "all": 90
    },
    "dt": 1485789600,
    "sys": {
        "type": 1,
        "id": 5091,
        "message": 0.0103,
        "country": "GB",
        "sunrise": 1485762037,
        "sunset": 1485794875
    },
    "id": 2643743,
    "name": "London",
    "cod": 200
}

 

要像这样的天气概述结构
type Weather struct {
    Location       string  
    Weather        string  
    Description    string  
    Temperature    float32 
    MinTemperature float32 
    MaxTemperature float32 
}



使用标准的JSON包,我们将解组它,然后像这样重组它

    type Weather struct {
        Location       string
        Weather        string
        Description    string
        Temperature    float32
        MinTemperature float32
        MaxTemperature float32
    }

    type TmpWeather struct {
        Location string `json:"name"`
        Weather  []struct {
            Weather     string `json:"main"`
            Description string `json:"description"`
        } `json:"weather"`
        Temperature struct {
            Temperature    float32 `json:"temp"`
            MinTemperature float32 `json:"temp_min"`
            MaxTemperature float32 `json:"temp_max"`
        } `json:"main"`
    }

    var tmpW TmpWeather
    err := json.Unmarshal([]byte(jsonString), &tmpW)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v
", tmpW)
    // {Location:London Weather:[{Weather:Drizzle Description:light intensity drizzle}] Temperature:{Temperature:280.32 MinTemperature:279.15 MaxTemperature:281.15}}

    weather := Weather{
        Location:       tmpW.Location,
        Weather:        tmpW.Weather[0].Weather,
        Description:    tmpW.Weather[0].Description,
        Temperature:    tmpW.Temperature.Temperature,
        MinTemperature: tmpW.Temperature.MinTemperature,
        MaxTemperature: tmpW.Temperature.MaxTemperature,
    }

    fmt.Printf("%+v
", weather)
    // {Location:London Weather:Drizzle Description:light intensity drizzle Temperature:280.32 MinTemperature:279.15 MaxTemperature:281.15}



njson标记添加到struct,
然后像这样使用NJSON解组

    type Weather struct {
        Location       string  `njson:"name"`
        Weather        string  `njson:"weather.0.main"`
        Description    string  `njson:"weather.0.description"`
        Temperature    float32 `njson:"main.temp"`
        MinTemperature float32 `njson:"main.temp_min"`
        MaxTemperature float32 `njson:"main.temp_max"`
    }

    var weather Weather
    err := njson.Unmarshal([]byte(jsonString), &weather)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v
", weather)
    // {Location:London Weather:Drizzle Description:light intensity drizzle Temperature:280.32 MinTemperature:279.15 MaxTemperature:281.15}

 

以上是关于Golang如何解组嵌套的JSON数据的子集的主要内容,如果未能解决你的问题,请参考以下文章

GOLANG 解组动态 JSON

当 JSON 字段键是日期时,如何将 JSON 对象解组为 Golang 结构?

golang中的动态嵌套结构

在 Golang 中解组 XML 时如何在 interface 中获取数据?

如何使用 golang 将未编组的 JSON 连接到 HTML 页面?

在 Golang 中将字符串解组为类似结构的结构