从响应中获取数组中的元素

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

get element from array from response

问题

我想从HTTP响应中获取数组的第二个元素,但我不明白如何做。

// 发送GET请求
response, error := http.Get("https://api.binance.com/api/v3/klines?symbol=ETHUSDT&interval=5m&limit=5")
if error != nil {
fmt.Println(error)
}

// 读取响应体
body, error := ioutil.ReadAll(response.Body)

if error != nil {
fmt.Println(error)
}

// 关闭响应体
response.Body.Close()

// 打印响应体
fmt.Println(string(body))

这是响应的内容:

/*
[[1684914900000,"1818.17000000","1818.17000000","1813.82000000","1814.07000000","2252.21430000",1684915199999,
"4088300.51162300",2231,"1056.16450000","1917074.16909000","0"],[1684915200000,"1814.07000000","1816.90000000","1813.35000000",
"1816.76000000","1721.60850000",1684915499999,"3124080.20094500",1984,"1016.47410000","1844514.03018800","0"],
[1684915500000,"1816.76000000","1817.08000000","1815.77000000","1816.66000000","774.00760000",1684915799999,"1405864.36999700",
1349,"382.31660000","694372.58708700","0"],[1684915800000,"1816.67000000","1816.67000000",
"1814.43000000","1816.19000000","1390.68620000",1684916099999,"2524716.97451500",1552,"457.95860000","831395.49368900","0"],
[1684916100000,"1816.19000000","1817.08000000","1815.80000000","1817.07000000","536.54530000",1684916399999,"974504.17830700",1047,"312.72070000","568003.96710900","0"]]
*/

我想从第二个数组中获取第二个元素。请帮助我解析数组并获取我需要的元素。

英文:

I want to get second element from array from an HTTP response, but I don't understand how.

        // make GET request
    	response, error := http.Get("https://api.binance.com/api/v3/klines?symbol=ETHUSDT&interval=5m&limit=5")
    	if error != nil {
    		fmt.Println(error)
    	}
    
    	// read response body
    	body, error := ioutil.ReadAll(response.Body)
    
    	if error != nil {
    		fmt.Println(error)
    	}
    
    	// close response body
    	response.Body.Close()
    
    	// print response body
    	fmt.Println(string(body))

This is respons:

/*

[[1684914900000,"1818.17000000","1818.17000000","1813.82000000","1814.07000000","2252.21430000",1684915199999,

"4088300.51162300",2231,"1056.16450000","1917074.16909000","0"],[1684915200000,"1814.07000000","1816.90000000","1813.35000000",

"1816.76000000","1721.60850000",1684915499999,"3124080.20094500",1984,"1016.47410000","1844514.03018800","0"],

[1684915500000,"1816.76000000","1817.08000000","1815.77000000","1816.66000000","774.00760000",1684915799999,"1405864.36999700",

1349,"382.31660000","694372.58708700","0"],[1684915800000,"1816.67000000","1816.67000000","

1814.43000000","1816.19000000","1390.68620000",1684916099999,"2524716.97451500",1552,"457.95860000","831395.49368900","0"],

[1684916100000,"1816.19000000","1817.08000000","1815.80000000","1817.07000000","536.54530000",1684916399999,"974504.17830700",1047,"312.72070000","568003.96710900","0"]]

*/

I want get second item from second Array. Please, help me to parse array and get elements that I need

答案1

得分: 1

以下是要翻译的内容:

示例项目 尝试使用这个 JsonResp 结构体来表示 JSON 响应的结构。然后进行 HTTP 请求,读取和解析响应体,并从 resp 中提取第二个元素。

请查看示例项目以更好地理解。

英文:

Sample Project tryout this one.JsonResp struct that represents the structure of the JSON response. then it makes the HTTP request, reads and parses the response body, and extracts the second element from the resp.

check out the sample project for a better understanding

答案2

得分: -1

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	response := `[[1684914900000,"1818.17000000","1818.17000000","1813.82000000","1814.07000000","2252.21430000",1684915199999,
"4088300.51162300",2231,"1056.16450000","1917074.16909000","0"],[1684915200000,"1814.07000000","1816.90000000","1813.35000000",
"1816.76000000","1721.60850000",1684915499999,"3124080.20094500",1984,"1016.47410000","1844514.03018800","0"],
[1684915500000,"1816.76000000","1817.08000000","1815.77000000","1816.66000000","774.00760000",1684915799999,"1405864.36999700",
1349,"382.31660000","694372.58708700","0"],[1684915800000,"1816.67000000","1816.67000000","1814.43000000","1816.19000000","1390.68620000",1684916099999,"2524716.97451500",1552,"457.95860000","831395.49368900","0"],
[1684916100000,"1816.19000000","1817.08000000","1815.80000000","1817.07000000","536.54530000",1684916399999,"974504.17830700",1047,"312.72070000","568003.96710900","0"]]
`
	var elements [][]interface{}
	err := json.Unmarshal([]byte(response), &elements)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%v", elements[1][1])
}
package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	response := `[[1684914900000,"1818.17000000","1818.17000000","1813.82000000","1814.07000000","2252.21430000",1684915199999,
"4088300.51162300",2231,"1056.16450000","1917074.16909000","0"],[1684915200000,"1814.07000000","1816.90000000","1813.35000000",
"1816.76000000","1721.60850000",1684915499999,"3124080.20094500",1984,"1016.47410000","1844514.03018800","0"],
[1684915500000,"1816.76000000","1817.08000000","1815.77000000","1816.66000000","774.00760000",1684915799999,"1405864.36999700",
1349,"382.31660000","694372.58708700","0"],[1684915800000,"1816.67000000","1816.67000000","1814.43000000","1816.19000000","1390.68620000",1684916099999,"2524716.97451500",1552,"457.95860000","831395.49368900","0"],
[1684916100000,"1816.19000000","1817.08000000","1815.80000000","1817.07000000","536.54530000",1684916399999,"974504.17830700",1047,"312.72070000","568003.96710900","0"]]
`
	var elements [][]interface{}
	err := json.Unmarshal([]byte(response), &elements)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%v", elements[1][1])
}
英文:
package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	response := `[[1684914900000,"1818.17000000","1818.17000000","1813.82000000","1814.07000000","2252.21430000",1684915199999,
"4088300.51162300",2231,"1056.16450000","1917074.16909000","0"],[1684915200000,"1814.07000000","1816.90000000","1813.35000000",
"1816.76000000","1721.60850000",1684915499999,"3124080.20094500",1984,"1016.47410000","1844514.03018800","0"],
[1684915500000,"1816.76000000","1817.08000000","1815.77000000","1816.66000000","774.00760000",1684915799999,"1405864.36999700",
1349,"382.31660000","694372.58708700","0"],[1684915800000,"1816.67000000","1816.67000000","1814.43000000","1816.19000000","1390.68620000",1684916099999,"2524716.97451500",1552,"457.95860000","831395.49368900","0"],
[1684916100000,"1816.19000000","1817.08000000","1815.80000000","1817.07000000","536.54530000",1684916399999,"974504.17830700",1047,"312.72070000","568003.96710900","0"]]
`
	var elements [][]interface{}
	err := json.Unmarshal([]byte(response), &elements)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%v", elements[1][1])
}

huangapple
  • 本文由 发表于 2023年5月24日 17:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322211.html
匿名

发表评论

匿名网友

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

确定