编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《使用 Golang 的 for 循环从 JSON 中打印每个项目的方法》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type response struct {
status string `json:"status"`
message string `json:"message"`
result []struct {
blocknumber string `json:"blocknumber"`
timestamp string `json:"timestamp"`
hash string `json:"hash"`
nonce string `json:"nonce"`
blockhash string `json:"blockhash"`
transactionindex string `json:"transactionindex"`
from string `json:"from"`
to string `json:"to"`
value string `json:"value"`
gas string `json:"gas"`
gasprice string `json:"gasprice"`
iserror string `json:"iserror"`
txreceiptstatus string `json:"txreceipt_status"`
input string `json:"input"`
contractaddress string `json:"contractaddress"`
cumulativegasused string `json:"cumulativegasused"`
gasused string `json:"gasused"`
confirmations string `json:"confirmations"`
} `json:"result"`
}
func getapi(wallet string) ([]byte, error) {
res, err := http.get("https://api.bscscan.com/api?module=account&action=txlist&address=" + wallet + "&startblock=0&endblock=99999999&sort=asc&apikey=5q4ss1xhv95sipsckk1fkcpgjsmrm2ei1k")
if err != nil {
log.fatal(err)
}
defer res.body.close()
return ioutil.readall(res.body)
}
func j_unm(body []byte) response {
var r response
err := json.unmarshal(body, &r)
if err != nil {
log.println("error unmarshalling json data:", err)
}
return r
}
func printitem(r response, jitem string) []string {
item := []string{}
for _, p := range r.result {
item = append(item, jitem+":"+p.jitem)
}
return item
}
package main
import "fmt"
func main() {
gettrx, _ := getapi("0xf287E9f40f96C0cbAEAdD29Cf22E6CF5cC66030b")
fmt.Println(printitem(j_unm(gettrx), "BlockNumber"))
}
我想将 jitem 作为变量传递。我收到以下错误。
jitem 定义为字符串,可以是 blocknumber 或 timestamp
p.jitem undefined (type struct{blocknumber string "json:\"blocknumber\""; timestamp string "json:\"timestamp\""; hash string "json:\"hash\""; nonce string " json:\"nonce\""; blockhash 字符串 "json:\"blockhash\""; transactionindex 字符串 "json:\"transactionindex\""; 从字符串 "json:\"from\""; 到字符串 "json: \"to\""; value 字符串 "json:\"value\""; gas 字符串 "json:\"gas\""; gasprice 字符串 "json:\"gasprice\""; iserror 字符串 "json:\" iserror\""; txreceiptstatus 字符串 "json:\"txreceipt_status\""; 输入字符串 "json:\"input\""; contractaddress 字符串 "json:\"contractaddress\""; cumulativegasused 字符串 "json:\"cumulativegasused\ ""; gasused 字符串 "json:\"gasused\""; 确认字符串 "json:\"confirmations\""} 没有字段或方法 jitem)compilermissingfieldormethod
正确答案
你想要这个吗?
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type response struct {
status string `json:"status"`
message string `json:"message"`
result []map[string]string `json:"result"`
}
func getapi(wallet string) ([]byte, error) {
res, err := http.get("https://api.bscscan.com/api?module=account&action=txlist&address=" + wallet + "&startblock=0&endblock=99999999&sort=asc&apikey=5q4ss1xhv95sipsckk1fkcpgjsmrm2ei1k")
if err != nil {
log.fatal(err)
}
defer res.body.close()
return ioutil.readall(res.body)
}
func j_unm(body []byte) response {
var r response
err := json.unmarshal(body, &r)
if err != nil {
log.println("error unmarshalling json data:", err)
}
return r
}
func printitem(r response, jitem string) []string {
item := []string{}
for _, p := range r.result {
item = append(item, jitem+":"+p[jitem])
}
return item
}
func main() {
gettrx, _ := getapi("0xf287e9f40f96c0cbaeadd29cf22e6cf5cc66030b")
fmt.println(printitem(j_unm(gettrx), "blocknumber"))
}
p.jitem 是字符串吗?如果此类型可转换为字符串,则使用 string(p.jitem)
func printitem(r Response, jItem string) []string {
//item := []string{}
items:= make([]string,0)
for _, p := range r.Result {
items = append(items, jItem+":"+ string(p.jItem))
}
return items以上就是《使用 Golang 的 for 循环从 JSON 中打印每个项目的方法》的详细内容,更多关于的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
