| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package common
- import (
- "encoding/json"
- "io/ioutil"
- "os"
- )
- type ReadData struct {
- Data interface{} `json:"data"`
- }
- type BaseModel struct {
- LaboratoryId int `json:"laboratory_id"` // 实验室ID
- LaboratoryName string `json:"laboratory_name"` // 实验室名称
- }
- // DeviceStatistics 实验设备使用数据
- type DeviceStatistics struct {
- BaseModel
- BaseEquipment int `json:"base_equipment"` // 基础科研设备
- LaboratoryEquipment int `json:"laboratory_equipment"` // 实验室设备
- UseTime int `json:"use_time"` // 使用时长
- ExperimentalData int `json:"experimental_data"` // 实验数据
- }
- // ReadJson 从配置文件中读取json
- func ReadJson(entity interface{}, path ...string) error {
- jPath := "data1.json"
- if len(path) > 0 {
- jPath = path[0]
- }
- // 打开json文件
- jsonFile, err := os.Open(jPath)
- if err != nil {
- return err
- }
- var rdata ReadData
- rdata.Data = &entity
- // 要记得关闭
- defer jsonFile.Close()
- byteValue, err := ioutil.ReadAll(jsonFile)
- if err != nil {
- return err
- }
- err = json.Unmarshal([]byte(byteValue), &rdata)
- if err != nil {
- return err
- }
- return nil
- }
|