Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解析cryptocompare API生成的JSON?_Json_Go - Fatal编程技术网

如何解析cryptocompare API生成的JSON?

如何解析cryptocompare API生成的JSON?,json,go,Json,Go,我试图使用这个API端点解析一些JSON { "Response": "Success", "Message": "Coin list succesfully returned! This api is moving to https://min-api.cryptocompare.com/data/all/coinlist, please change the path.", "BaseImageUrl": "https://www.cryptocompare.com", "BaseLink

我试图使用这个API端点解析一些JSON

 {
"Response": "Success",
"Message": "Coin list succesfully returned! This api is moving to https://min-api.cryptocompare.com/data/all/coinlist, please change the path.",
"BaseImageUrl": "https://www.cryptocompare.com",
"BaseLinkUrl": "https://www.cryptocompare.com",
"DefaultWatchlist": {
"CoinIs": "1182,7605,5038,24854,3807,3808,202330,5324,5031,20131",
"Sponsored": ""
},
"Data": {
"42": {
"Id": "4321",
"Url": "/coins/42/overview",
"ImageUrl": "/media/12318415/42.png",
"Name": "42",
"Symbol": "42",
"CoinName": "42 Coin",
"FullName": "42 Coin (42)",
"Algorithm": "Scrypt",
"ProofType": "PoW/PoS",
"FullyPremined": "0",
"TotalCoinSupply": "42",
"PreMinedValue": "N/A",
"TotalCoinsFreeFloat": "N/A",
"SortOrder": "34",
"Sponsored": false
},

我可以看到它使请求变得很好,然后我尝试解码响应的主体,它返回大量随机数

如果我从我的调试器复制主体值,我会得到以下结果

(长度:643401,上限:1048064)

这是我的密码

url := fmt.Sprintf("https://www.cryptocompare.com/api/data/coinlist/")

    fmt.Println("Requesting data from " + url)

    req, err := http.NewRequest("GET", url, nil)

    if err != nil {
        log.Fatal("NewRequest: ", err)
        return
    }

    client := &http.Client{}

    resp, err := client.Do(req)

    if err != nil {
        log.Fatal("Do: ", err)
        return
    }

    body, readErr := ioutil.ReadAll(resp.Body)
我希望能够从JSON中获取数据键内部的所有内容,然后将其映射到结构。有人能看出我做错了什么吗

下面是我点击端点时在浏览器中看到的示例

 {
"Response": "Success",
"Message": "Coin list succesfully returned! This api is moving to https://min-api.cryptocompare.com/data/all/coinlist, please change the path.",
"BaseImageUrl": "https://www.cryptocompare.com",
"BaseLinkUrl": "https://www.cryptocompare.com",
"DefaultWatchlist": {
"CoinIs": "1182,7605,5038,24854,3807,3808,202330,5324,5031,20131",
"Sponsored": ""
},
"Data": {
"42": {
"Id": "4321",
"Url": "/coins/42/overview",
"ImageUrl": "/media/12318415/42.png",
"Name": "42",
"Symbol": "42",
"CoinName": "42 Coin",
"FullName": "42 Coin (42)",
"Algorithm": "Scrypt",
"ProofType": "PoW/PoS",
"FullyPremined": "0",
"TotalCoinSupply": "42",
"PreMinedValue": "N/A",
"TotalCoinsFreeFloat": "N/A",
"SortOrder": "34",
"Sponsored": false
},

我不确定我是否正确理解了您的问题,但以下是我将如何解析API给出的响应:

package main

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

type Data struct {
    Id        string
    CoinName  string
    Algorithm string
    // You can add more properties if you need them
}

type Response struct {
    Response         string
    Message          string
    BaseImageUrl     string
    BaseLinkUrl      string
    DefaultWatchlist map[string]string
    // If you're interested in the Data only all of the above properties can be excluded
    Data             map[string]Data
}

func main() {
    url := "https://min-api.cryptocompare.com/data/all/coinlist"
    resp := Response{}

    // Getting the data using http
    r, err := http.Get(url)
    if err != nil {
        log.Fatal(err.Error())
    }

    // Reading the response body using ioutil
    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        log.Fatal(err.Error())
    }
    defer r.Body.Close()

    if r.StatusCode == http.StatusOK {
        // Unmarshaling the json bytes into Response struct
        json.Unmarshal(body, &resp)

        fmt.Println(resp.Data) // Printing parsed struct
    }
}

“然后,我尝试解码响应的主体,它会返回大量随机数。”


这些随机数是由
ioutil.ReadAll()
方法给出的一个字节片
[]byte
的响应体,它非常适合解组

所示代码中的所有内容看起来都很好。显示解析JSON的代码。显示随机数及其显示方式。一半代码可以替换为
resp,err:=http.Get(url)
您尚未显示如何解码数据,或“随机数”的含义。请创建一个。非常感谢!正是我想要的。@NickPoock很高兴我能帮助你:]快乐的地鼠!