小编M.A*_*A.G的帖子

golang中Json转字符串

我想做的是将我从第三方 API 获得的 JSON 响应转换为字符串,以便能够在网页上呈现它。我首先尝试创建一个名为 的结构体money,其中包含要返回的 3 个值,然后Unmarshel是字节,但我没有显示任何内容

这是结构

type money struct {
Base     string  `json:"base"`
Currency string  `json:"currency"`
Amount   float32 `json:"amount"`}
Run Code Online (Sandbox Code Playgroud)

并在getCurrency()函数内部

    response, err := http.Get("https://api.coinbase.com/v2/prices/spot?currency=USD")

if err != nil {
    fmt.Printf("The http requst failed with error %s \n", err)
} else {
    answer, _ := ioutil.ReadAll(response.Body)
    response := money{}
    json.Unmarshal([]byte(answer), &response)
    fmt.Fprintln(w, response)
    fmt.Fprintln(w, response.Currency)


}
Run Code Online (Sandbox Code Playgroud)

最后这是我从 json 响应中得到的结果

 {"data":{"base":"BTC","currency":"USD","amount":"4225.87"}}
Run Code Online (Sandbox Code Playgroud)

json http go unmarshalling

5
推荐指数
1
解决办法
6万
查看次数

如何根据第一个字母后的数字对文本文件进行排序

我在 Linux 中有一个文本文件,如下所示:

H-988   -0.5418829321861267 no
H-989   -0.5033702254295349 yes
H-990   -1.1516857147216797 hi
H-99    -0.5005123019218445 hello
Run Code Online (Sandbox Code Playgroud)

我想根据连字符后面的数字对该文件进行排序。所以顺序应该是:

H-99    -0.5005123019218445 hello
H-988   -0.5418829321861267 no
H-989   -0.5033702254295349 yes
H-990   -1.1516857147216797 hi
Run Code Online (Sandbox Code Playgroud)

我尝试了 grepsort命令,但没有成功。例如,它将 95 放在 949 之后,而不是之前,对于 99 和 990 也是如此,如提供的示例所示

linux sorting grep

4
推荐指数
1
解决办法
701
查看次数

标签 统计

go ×1

grep ×1

http ×1

json ×1

linux ×1

sorting ×1

unmarshalling ×1