如何提取特定的JSON数据

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

How to extract specific JSON data

问题

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

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

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

package main 

import(
    "fmt"
	"io"
	"log"
	"net/http"
)

type Product struct {
	ID string `json:"id"`
}

func get_single_product() {

	res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")

	if err != nil {
		log.Fatal(err)
	}

	data, err := io.ReadAll(res.Body)
	res.Body.Close()

	if err != nil {
		log.Fatal(err)
	}

	product := Product{}
	err = json.Unmarshal(data, &product)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", product.ID)

}

func main() {

	// 调用获取产品函数
	get_single_product()
}

这将返回...

{
	"id": "UNIUSD"
}
英文:

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.

package main 

import(
    "fmt"
	"io"
	"log"
	"net/http"
)

func get_single_product() {

	res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")

	if err != nil {
		log.Fatal(err)
	}

	data, err := io.ReadAll(res.Body)
	res.Body.Close()

	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", data)

}

func main() {

	// CALL GET PRODUCTS FUNCTION
	get_single_product()
}

This Returns...

{
"id":"UNIUSD",
"base_currency":"UNI",
"quote_currency":"USD",
"base_min_size":"0.1",
"base_max_size":"200000",
"quote_increment":"0.0001",
"base_increment":"0.000001",
"display_name":"UNI/USD",
"min_market_funds":"1.0",
"max_market_funds":"100000",
"margin_enabled":false,
"post_only":false,
"limit_only":false,
"cancel_only":false,
"trading_disabled":false,
"status":"online",
"status_message":""
}

答案1

得分: 1

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

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

type Response struct {
	ID string `json:"id"`
}

func main() {
	res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")
	if err != nil {
		log.Fatal(err)
	}

	var response Response
	json.NewDecoder(res.Body).Decode(&response)

	fmt.Println(response.ID)
}

// 输出
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().

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

type Response struct {
	ID string `json:"id"`
}

func main() {
	res, err := http.Get("https://api.pro.coinbase.com/products/UNI-USD")
	if err != nil {
		log.Fatal(err)
	}

	var response Response
	json.NewDecoder(res.Body).Decode(&response)

	fmt.Println(response.ID)
}

// Output
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:

确定