从JSON文件中读取数组到你的GO变量中。

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

read arrays from a JSON file to your GO variables

问题

我被指派将我的项目从Python转换为Go,但我已经在这个问题上困惑了几个小时。

我有一个名为data.json的文件,其中包含以下数据:

  1. {
  2. "mobiles":["iPhone 13 Pro","OnePlus 10 Pro","Google Pixel 6"],
  3. "laptops":["Dell XSP 13","Acer Chromebook Spin","Lenovo ThinkPad x1"],
  4. "cars":["Suzuki Crossover","Golf GTI","Hyundai Tucson","Hyundai Kona"]
  5. }

对于我的项目,我想将data.json中的数据加载到以下切片中:

  1. mobiles := []{从json中获取的数据}
  2. laptops := []{从json中获取的数据}
  3. cars := []{从json中获取的数据}

我在考虑使用切片,如果我没错的话,这样做是否允许我根据需要向我的json文件中的数组添加更多数据?

  1. jsonFile, err := os.Open("data/data.json")
  2. if err != nil {
  3. fmt.Println(err)
  4. }
  5. fmt.Println(jsonFile)
  6. fmt.Println("Successfully Opened data.json")

这给我输出:
&{0xc000062180}
Successfully Opened data.json

我试图使用以下方式获取我需要的数据:

  1. fmt.Println(jsonFile["cars"])

我需要学习指针吗?还是我完全走错了方向?

英文:

I have been tasked with converting my project from Python to Go, but I've been stumped for hours on this.

I have this file data.json which contains the following data

  1. {
  2. "mobiles":["iPhone 13 Pro","OnePlus 10 Pro","Google Pixel 6"],
  3. "laptops":["Dell XSP 13","Acer Chromebook Spin","Lenovo ThinkPad x1"],
  4. "cars":["Suzuki Crossover","Golf GTI","Hyundai Tucson","Hyundai Kona"]
  5. }

For my project, I would like to load the data from data.json into the following slices

  1. mobiles := []{ data from json }
  2. laptops := []{ data from json }
  3. cars := []{ data from json }

I'm thinking of using slices, if I'm not wrong would that allow me to add more data to my arrays in the json file as I need?

  1. jsonFile, err := os.Open("data/data.json")
  2. if err != nil {
  3. fmt.Println(err)
  4. }
  5. fmt.Println(jsonFile)
  6. fmt.Println("Successfully Opened data.json")

This gives me:
&{0xc000062180}
Successfully Opened data.json

I was trying to target the data I need with for example:

  1. fmt.Println(jsonFile["cars"])

Do I need to learn about pointers or am I completly barking up the wrong tree?

答案1

得分: 1

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. )
  8. type Todo struct {
  9. Mobiles []string `json:"mobiles"`
  10. Laptops []string `json:"laptops"`
  11. Cars []string `json:"cars"`
  12. }
  13. func main() {
  14. jsonFile, err := os.Open("file.json")
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer jsonFile.Close()
  19. byteValue, err := ioutil.ReadAll(jsonFile)
  20. if err != nil {
  21. panic(err)
  22. }
  23. var todo Todo
  24. json.Unmarshal([]byte(byteValue), &todo)
  25. fmt.Println(todo)
  26. fmt.Println(todo.Cars)
  27. }

第一个输出是你的 JSON 文件

{[iPhone 13 Pro OnePlus 10 Pro Google Pixel 6] [Dell XSP 13 Acer Chromebook Spin Lenovo ThinkPad x1] [Suzuki Crossover Golf GTI Hyundai Tucson Hyundai Kona]}

第二个输出是汽车列表

[Suzuki Crossover Golf GTI Hyundai Tucson Hyundai Kona]

英文:
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. )
  8. type Todo struct {
  9. Mobiles []string `json:"mobiles"`
  10. Laptops []string `json:"laptops"`
  11. Cars []string `json:"cars"`
  12. }
  13. func main() {
  14. jsonFile, err := os.Open("file.json")
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer jsonFile.Close()
  19. byteValue, err := ioutil.ReadAll(jsonFile)
  20. if err != nil {
  21. panic(err)
  22. }
  23. var todo Todo
  24. json.Unmarshal([]byte(byteValue), &todo)
  25. fmt.Println(todo)
  26. fmt.Println(todo.Cars)
  27. }

the first output will be your json file

> {[iPhone 13 Pro OnePlus 10 Pro Google Pixel 6] [Dell XSP 13 Acer
> Chromebook Spin Lenovo ThinkPad x1] [Suzuki Crossover Golf GTI Hyundai
> Tucson Hyundai Kona]}

and the second the cars

> [Suzuki Crossover Golf GTI Hyundai Tucson Hyundai Kona]

huangapple
  • 本文由 发表于 2022年10月31日 23:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/74265470.html
匿名

发表评论

匿名网友

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

确定