0
獲取值我有以下一段這就要求雅虎財經API來獲取給定的股票代碼股票值的代碼。如何從JSON在golang
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
//Response structure
type Response struct {
Query struct {
Count int `json:"count"`
Created string `json:"created"`
Lang string `json:"lang"`
Results struct {
Quote []struct {
LastTradePriceOnly string `json:"LastTradePriceOnly"`
} `json:"quote"`
} `json:"results"`
} `json:"query"`
}
func main() {
var s Response
response, 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.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
json.Unmarshal([]byte(contents), &s)
fmt.Println(s.Query.Results.Quote)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(contents))
}
}
fmt.Println(s.Query.Results.Quote)是給我一個多值序列,因爲引用是對結構的陣列。對於例如:[{52.05},{} 114.25] 我應該如何在golang一個值拆呢? 例如:52.05 114.25
幫助受到高度讚賞。 感謝
你提到你是新的,所以注意一定,如果你知道這一點,但你可以使用:爲_,Q:=範圍s.Query.Results.Quote {} q.LastTradePriceOnly(作爲一種簡單的迭代)。同樣,如果您LastTradePriceOnly類型float64(或FLOAT32),它會分析到數字形式。不這樣做,如果它有時是爲響應大衛非數字 –
感謝。我會考慮它:) –