无法将 json 数据反序列化为结构

Not able to deserialize json data into structs

我是一个新手,正在尝试编写一个简单的应用程序来从 greatschools.org 中提取一些学校数据。 json 数据如下所示:

{
    "schools": {
        "school": [
            {
                "gsId": 1,
                "name": "Catholic School",
                "type": "private",
                "gradeRange": "PK-9",
                "enrollment": 39,
                "parentRating": 4,
                "city": "Denver",
                "state": "CO",
                "address": "111 Main St., \nDenver, CO  80100",
                "phone": "(720) 555-1212",
                "fax": "(720) 555-1212",
                "website": "http://www.myschool.org",
                "ncesId": "1234567",
                "lat": 30.519446,
                "lon": -105.71314,
                "overviewLink": "http://www.greatschools.org/colorado/Denver/1-Catholic-School/?s_cid=gsapi",
                "ratingsLink": "http://www.greatschools.org/school/rating.page?state=CO&id=1&s_cid=gsapi",
                "reviewsLink": "http://www.greatschools.org/school/parentReviews.page?state=CO&id=1&s_cid=gsapi",
                "schoolStatsLink": "http://www.greatschools.org/cgi-bin/CO/otherprivate/1"
            }...

我的结构是这样的:

type SchoolStruct struct {
    GsId            int
    Name            string
    SchoolType      string
    GradeRange      string
    Enrollment      int
    ParentRating    int
    City            string
    State           string
    Address         string
    Phone           string
    Fax             string
    Website         string
    NcesId          string
    Lat             float64
    Lon             float64
    OverviewLink    string
    RatingsLink     string
    ReviewsLink     string
    SchoolStatsLink string
}

type SchoolsStruct struct {
    Schools []SchoolStruct
}

当我 运行 我的代码时,我得到 "json: cannot unmarshal object into Go value of type []main.SchoolStruct"

我正在使用 gopencils 库来发出我的请求,并且已经成功地使用它处理了非常简单的请求。你知道我可能做错了什么吗?

有一些问题。对于初学者,您正在尝试解码学校,但数据是学校,其中有一个成员(数组)学校。

另一个问题是您的名字不匹配。 GsId != gsid 除非你用结构体标签告诉 Go。

改为尝试解码为 SchoolResponseData:

type SchoolResponseData struct {
    Schools struct {
        School []struct {
            Address         string  `json:"address"`
            City            string  `json:"city"`
            Enrollment      float64 `json:"enrollment"`
            Fax             string  `json:"fax"`
            GradeRange      string  `json:"gradeRange"`
            GsId            float64 `json:"gsId"`
            Lat             float64 `json:"lat"`
            Lon             float64 `json:"lon"`
            Name            string  `json:"name"`
            NcesId          string  `json:"ncesId"`
            OverviewLink    string  `json:"overviewLink"`
            ParentRating    float64 `json:"parentRating"`
            Phone           string  `json:"phone"`
            RatingsLink     string  `json:"ratingsLink"`
            ReviewsLink     string  `json:"reviewsLink"`
            SchoolStatsLink string  `json:"schoolStatsLink"`
            State           string  `json:"state"`
            Type            string  `json:"type"`
            Website         string  `json:"website"`
        } `json:"school"`
    } `json:"schools"`
}