我有以下 JSON 文件并尝试解析它。
{
"coord":
{
"lon":-121.31,
"lat":38.7},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01d"}
],
"base":"stations",
"main":
{
"temp":73.26,
"pressure":1018,
"humidity":17,
"temp_min":68,
"temp_max":77},
预期输出为:
当前温度:73
今日最低点:68
今日最高点:77
当前湿度:17%
但它反而返回:
当前温度:0
今天的最低价:0
今天的最高价:0
当前湿度:0%
这是我试图用来获得所需回报的代码:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
type Daily struct {
Currenttemp int `json:"temp"`
Mintemp int `json:"temp_min"`
Maxtemp int `json:"temp_max"`
Humidity int `json:"humidity"`
}
func main() {
jsonFile, err := os.Open("jsontest1.json")
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened jsontest1.json")
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var daily Daily
json.Unmarshal(byteValue, &daily)
fmt.Println("Current Temperature:"+strconv.Itoa(daily.Currenttemp))
fmt.Println("Today's Low:"+strconv.Itoa(daily.Mintemp))
fmt.Println("Today's High:"+strconv.Itoa(daily.Maxtemp))
fmt.Println("Current Humidity:"+strconv.Itoa(daily.Humidity)+"%")
}
我缺少什么?
侃侃无极
相关分类