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

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

read arrays from a JSON file to your GO variables

问题

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

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

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

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

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

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

jsonFile, err := os.Open("data/data.json")	
if err != nil {
    fmt.Println(err)
}
fmt.Println(jsonFile)
fmt.Println("Successfully Opened data.json")

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

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

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

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

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

mobiles := []{ data from json }
laptops := []{ data from json }
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?

	jsonFile, err := os.Open("data/data.json")	
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(jsonFile)
	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:

fmt.Println(jsonFile["cars"])

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

答案1

得分: 1

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

type Todo struct {
	Mobiles []string `json:"mobiles"`
	Laptops  []string `json:"laptops"`
	Cars  []string `json:"cars"`
}

func main() {
	jsonFile, err := os.Open("file.json")
	if err != nil {
		panic(err)
	}
	
	defer jsonFile.Close()

	byteValue, err := ioutil.ReadAll(jsonFile)
	if err != nil {
		panic(err)
	}

	var todo Todo
	json.Unmarshal([]byte(byteValue), &todo)

	fmt.Println(todo)
	fmt.Println(todo.Cars)
}

第一个输出是你的 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]

英文:
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

type Todo struct {
	Mobiles []string `json:"mobiles"`
	Laptops  []string `json:"laptops"`
	Cars  []string `json:"cars"`
}

func main() {
	jsonFile, err := os.Open("file.json")
	if err != nil {
		panic(err)
	}
	
	defer jsonFile.Close()

	byteValue, err := ioutil.ReadAll(jsonFile)
	if err != nil {
		panic(err)
	}

	var todo Todo
	json.Unmarshal([]byte(byteValue), &todo)

	fmt.Println(todo)
	fmt.Println(todo.Cars)
}

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:

确定