如何在golang中从JSON中获取值

huangapple go评论91阅读模式
英文:

How to fetch values from JSON in golang

问题

我有以下一段代码,它调用Yahoo Finance API来获取给定股票代码的股票价值。

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
)

// Response 结构体
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) 返回一个多值数组,因为 Quote 是一个结构体数组。例如:[{52.05},{114.25}]
在 Golang 中,如何将它拆分为单个值?
例如:52.05114.25

非常感谢您的帮助。

英文:

i have following piece of code which calls the yahoo finance api to get the stock values for given stock symbol.

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) is giving me a multi value array since Quote is a array of structure. For eg: [{52.05},{114.25}]
How should i split it in a single value in golang ?
For eg: 52.05
114.25

Help is highly appreciated.
Thanks

答案1

得分: 1

我是新手,对于很多数据结构都不太了解。但是我找到了如何从结构体数组中获取单个值的方法。

fmt.Println(s.Query.Results.Quote[0].LastTradePriceOnly)

这对我起作用了。我只需要在循环中迭代这个操作来获取所有的值。

谢谢。

英文:

I am new to golang and not aware of many data structures. But i figured out how to get the single value out of array of structure.

fmt.Println(s.Query.Results.Quote[0].LastTradePriceOnly)

this worked for me..I only have to iterate this in a loop to fetch all values.

Thanks.

huangapple
  • 本文由 发表于 2015年9月26日 07:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/32791864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定