如何提取特定的JSON数据

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

How to extract specific JSON data

问题

我不知道如何选择特定的JSON数据。

您可以如何更改此代码,以便仅获取id,并且没有其他响应数据?

我在网上阅读了一些资料,显然我需要使用一个结构体?我不确定如何解决这个问题。

  1. package main
  2. import(
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. )
  8. type Product struct {
  9. ID string `json:"id"`
  10. }
  11. func get_single_product() {
  12. res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. data, err := io.ReadAll(res.Body)
  17. res.Body.Close()
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. product := Product{}
  22. err = json.Unmarshal(data, &product)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Printf("%s", product.ID)
  27. }
  28. func main() {
  29. // 调用获取产品函数
  30. get_single_product()
  31. }

这将返回...

  1. {
  2. "id": "UNIUSD"
  3. }
英文:

I do not know how to select specific JSON data.

How can I change this code so that I have just the id, and none of the other response data?

I read online, and apparently I need to use a struct? I am unsure of how to approach this problem.

  1. package main
  2. import(
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. )
  8. func get_single_product() {
  9. res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. data, err := io.ReadAll(res.Body)
  14. res.Body.Close()
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. fmt.Printf("%s", data)
  19. }
  20. func main() {
  21. // CALL GET PRODUCTS FUNCTION
  22. get_single_product()
  23. }

This Returns...

  1. {
  2. "id":"UNIUSD",
  3. "base_currency":"UNI",
  4. "quote_currency":"USD",
  5. "base_min_size":"0.1",
  6. "base_max_size":"200000",
  7. "quote_increment":"0.0001",
  8. "base_increment":"0.000001",
  9. "display_name":"UNI/USD",
  10. "min_market_funds":"1.0",
  11. "max_market_funds":"100000",
  12. "margin_enabled":false,
  13. "post_only":false,
  14. "limit_only":false,
  15. "cancel_only":false,
  16. "trading_disabled":false,
  17. "status":"online",
  18. "status_message":""
  19. }

答案1

得分: 1

你可以使用encoding/json包将你的JSON数据解码为一个对象。只需在对象中定义你感兴趣的字段,它将只读取这些字段。然后使用json.NewDecoder()创建一个解码器。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. type Response struct {
  9. ID string `json:"id"`
  10. }
  11. func main() {
  12. res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. var response Response
  17. json.NewDecoder(res.Body).Decode(&response)
  18. fmt.Println(response.ID)
  19. }
  20. // 输出
  21. UNI-USD

参考这个问题:https://stackoverflow.com/questions/17156371/how-to-get-json-response-from-http-get

英文:

You can use the encoding/json package to decode your json data to an object. Just define the fields you are interested in the object and it will only read out those. Then create a decoder with json.NewDecoder().

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. type Response struct {
  9. ID string `json:"id"`
  10. }
  11. func main() {
  12. res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. var response Response
  17. json.NewDecoder(res.Body).Decode(&response)
  18. fmt.Println(response.ID)
  19. }
  20. // Output
  21. UNI-USD

See also this question: https://stackoverflow.com/questions/17156371/how-to-get-json-response-from-http-get

huangapple
  • 本文由 发表于 2021年5月30日 04:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/67755344.html
匿名

发表评论

匿名网友

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

确定