您的位置:首页 >如何在 Go 语言中正确解析并提取 JSON 数组中的嵌套字段值
发布于2026-04-01 阅读(0)
扫一扫,手机访问

本文详解如何使用 Go 的 encoding/json 包解析嵌套 JSON 响应,安全访问结构体数组中的字段(如股票价格),并通过循环遍历提取所有值,避免 panic 和空指针错误。
本文详解如何使用 Go 的 `encoding/json` 包解析嵌套 JSON 响应,安全访问结构体数组中的字段(如股票价格),并通过循环遍历提取所有值,避免 panic 和空指针错误。
在 Go 中处理外部 API 返回的 JSON 数据时,常需从深层嵌套结构中提取特定字段。以 Yahoo Finance API 为例,其响应中 quote 是一个 JSON 数组,对应 Go 中的切片([]struct{})。直接访问 s.Query.Results.Quote[0].LastTradePriceOnly 虽可获取首个值,但若未校验切片长度或字段有效性,程序极易 panic。
首先,优化原始结构体定义,提升可读性与可维护性:
type Response struct {
Query struct {
Count int `json:"count"`
Created string `json:"created"`
Lang string `json:"lang"`
Results struct {
Quotes []Quote `json:"quote"` // 重命名 + 提升为独立类型
} `json:"results"`
} `json:"query"`
}
type Quote struct {
LastTradePriceOnly string `json:"LastTradePriceOnly"`
}接着,在 main() 中进行完整错误处理与安全遍历:
func main() {
resp, err := http.Get("http://query.yahooapis.com/v1/public/yql?q=select%20LastTradePriceOnly%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22,%22FB%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
if err != nil {
fmt.Fprintf(os.Stderr, "HTTP request failed: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read response body: %v\n", err)
os.Exit(1)
}
var s Response
if err := json.Unmarshal(body, &s); err != nil {
fmt.Fprintf(os.Stderr, "JSON unmarshal failed: %v\n", err)
os.Exit(1)
}
// ✅ 安全遍历:先检查切片长度,再逐项访问
quotes := s.Query.Results.Quotes
if len(quotes) == 0 {
fmt.Println("No stock quotes returned.")
return
}
fmt.Print("Stock prices: ")
for i, q := range quotes {
if q.LastTradePriceOnly != "" { // 防空字符串(部分 API 字段可能缺失)
if i > 0 {
fmt.Print(" ")
}
fmt.Print(q.LastTradePriceOnly)
}
}
fmt.Println()
}提取 JSON 数组字段的核心是:精准建模 → 完整错误处理 → 安全索引/遍历。避免硬编码下标(如 [0]),始终将切片视为动态集合;结合 range 循环与空值检查,即可健壮地提取所有目标值(如 52.05 114.25)。这是 Go 中处理第三方 JSON API 的标准实践。
下一篇:易企秀如何添加文字教程
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9