duan19911992 2015-03-04 22:02
浏览 51
已采纳

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

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:

{
    "schools": {
        "school": [
            {
                "gsId": 1,
                "name": "Catholic School",
                "type": "private",
                "gradeRange": "PK-9",
                "enrollment": 39,
                "parentRating": 4,
                "city": "Denver",
                "state": "CO",
                "address": "111 Main St., 
Denver, 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"
            }...

My structs look like this:

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
}

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条回答 默认 最新

  • donglan6967 2015-03-04 22:14
    关注

    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:"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"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理