无法将 JSON 数据反序列化为结构体。

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

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库进行请求,并且在非常简单的请求中已经成功使用过。你看出我可能做错了什么吗?

英文:

I'm a newbie to go and trying to write a simple app to pull some school data from greatschools.org. The json data looks like this:

<pre>{
"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"
}...</pre>

My structs look like this:

<pre>
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
}</pre>

When I run my code I get "json: cannot unmarshal object into Go value of type []main.SchoolStruct"

I'm using the gopencils library to make my requests and have used it successfully with very simple requests. Do you see what I might be doing wrong?

答案1

得分: 3

有几个问题。首先,你试图解码一个学校,但数据是schools,它有一个成员(一个数组)school。

另一个问题是你的名称不匹配。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"`
}
英文:

There are a few problems. For starters you are trying to decode a school, but the data is schools, which has a member (an array) of school.

Another issue is you have mismatched names. GsId != gsid unless you tell Go about it by using struct tags.

Try decoding to a SchoolResponseData instead:

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

huangapple
  • 本文由 发表于 2015年3月5日 06:02:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/28865899.html
匿名

发表评论

匿名网友

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

确定