dousuohe5882 2015-07-24 09:41
浏览 60
已采纳

将JSON文件解析为struct golang

I have this JSON file:

    {
  "AHSS": {
    "Lifelong Learning": "1sVhClGzmD5N_S6wGiS9_xHj2IkVgSv_un0rktvH2Goo",
    "TEST Learning": "1sdfVhClGzmD5N_S6wGiS9_xHj2ffgIkVgSv_un0rko56545o",
    "TEST Learning2": "fg1ac_PiSaWzeL3bA3tjWYs23dh61sVhClHj2IkVgSv_un0rktvH2Goo",
    "TEST Learning3": "13444sVhClGzmD5N_S6wGiS9_xHj2IkVgSv_un0rktyutvH2Goo",
    "TEST Learning4": "6657681sVhClGzmD5N_S6wGiS9_xHj2IkVgSv_un0rktvH2Goo"
  },
  "ProfServices": {
    "IT": "1fgac_PiSaWzeLxxdVuZs3bA3tjWY345d",
    "SomethingElse": "1ac_Pi3bA3tjWY4563",
    "Foo": "12ac_PiVuZs3bA3tjWYghfgj",
    "Bar": "445341ac_PiSaWzeLxA3tjWY54",
    "School1": "fg1ac_PiSaWzeL3bA3tjWYs23dh6",
    "School2": "fg341ac_PiSaWzeLZs3bA3tjWYsd4",
    "School3": "fgdf1ac_PiSaWzeLuZs3bA3tjWdgfY"    
  },
  "CollegeOfEngineering": {
    "Maths": "asdasdasdas45",
    "Physics": "pa6asa_Asddg",
    "Astrophysics": "asdfdasdasda",
    "School5": "mykeyyslaksdlkasmdlka",
    "School6": "asdasdkeykeykeykey"
  },
  "AnotherCollege": {
    "School7": "f111g1a2c_PiSaWzeL3bA3tjWYsdh6",
    "School8": "f4434234g341ac_PiSafgdfgdfgWzeLZs3bA3tjWYsd4",
    "School9": "fg23df1ac_PiSaWzeLuZs3bA3tjWdgfY"
  }
}

This is just an example to test with, but my actual json file will have a lot more data. But the format will always be the same. I will have an array of 'Colleges' such as 'AHSS' and ProfServices'. Within these Colleges are 'Schools' such as 'IT' or ' Lifeling Learning'. Each school has its own unique key. I need to read this JSON file and convert it into a struct.

I've tried looking around and using both json.Decode and json.Unmarshal and I'm really not sure how to get what I need. Everything I've tried prints out an empty struct. Maybe the layout of my struct in go is wrong?

Here is the struct layout I am using (not 100% this is correct):

    // Sheets struct will import from sheets.json
    Sheets struct {
        Colleges []struct {
            SheetKeys []string
        }
    }
)

And here are two methods I tried using to achieve my goal of importing the json file into a struct.

1)

sheetData, err := os.Open("sheets.json")
if err != nil {
    log.Fatalln(err)
}

jsonParser := json.NewDecoder(sheetData)
s := Sheets{}
jsonParser.Decode(&s)
log.Println(s)

2)

sheetData, err := ioutil.ReadFile("sheets.json")
s := Sheets{}
err = json.Unmarshal(sheetData, &s)
if err != nil {
    log.Fatalln(err)
}

log.Println(s)

Can anybody provide a method of importing this json file correctly? I'm not sure if its a problem with my method of doing it, or if its a problem with the struct layout.

Thanks.

  • 写回答

2条回答 默认 最新

  • duanbeng6709 2015-07-24 10:29
    关注

    You have an object of objects of strings. This unmarshals most naturally into

    map[string]map[string]string
    

    Then just work with it as you would with any other map.

    Playground: http://play.golang.org/p/_IX8WUDn4b.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,