golang读取json配置文件

test_json.go:

package main

import (

io "io/ioutil"

json "encoding/json"

"fmt"

)

type JsonStruct struct{

}

func NewJsonStruct () *JsonStruct {

return &JsonStruct{}

}

func (self *JsonStruct) Load (filename string, v interface{}) {

data, err := io.ReadFile(filename)

if err != nil{

return

}

datajson := []byte(data)

err = json.Unmarshal(datajson, v)

if err != nil{

return

}

}

type ValueTestAtmp struct{

StringValue string

NumericalValue int

BoolValue bool

}

type testdata struct {

ValueTestA ValueTestAtmp

}

func main() {

JsonParse := NewJsonStruct()

v := testdata{}

JsonParse.Load("jsonparse_config.txt", &v)

fmt.Println(v)

fmt.Println(v.ValueTestA .StringValue )

}

jsonparse_config.txt:

{

"ValueTestA":{

"StringValue": "127.1.1.1",

"NumericalValue":1233,

"BoolValue":false

},

"ValueTestB":{

"FloatValue":123.456

}

}

时间: 2024-08-10 22:07:38

golang读取json配置文件的相关文章

.Net Core控制台应用加载读取Json配置文件

⒈添加依赖 Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.FileExtensions Microsoft.Extensions.Configuration.Json ⒉在项目中添加Json配置文件并将其复制到输出目录属性设置为“始终复制” 1 { 2 "ConnectionStrings": { 3 "StudyConnStr": "Data Source=.;

python写的读取json配置文件

配置文件默认为conf.json 使用函数set完成追回配置项. 使用load或取配置项. 代码如下: #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' json配置文件类,调用方法 data_dict = {"a":"1", "b":"2"} JsonConf.set(data_dict) 即可在当前目录下生成json文件:config.json ''' import js

.Net Core Web应用加载读取Json配置文件

⒈添加Json配置文件并将“复制到输出目录”属性设置为“始终复制” 1 { 2 "Logging": { 3 "LogLevel": { 4 "Default": "Warning" 5 } 6 }, 7 "AllowedHosts": "*" 8 } 1 { 2 "ConnectionStrings": { 3 "StudyConnStr": &

net core 简单读取json配置文件

//添加 json 文件路径 var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); //创建配置根对象 var configurationRoot = builder1.Build(); //读取到ioc IOptions<SuperAppsettings> setting 依赖注入 serv

MOOC(7)- case依赖、读取json配置文件进行多个接口请求-openpyxl读取excel(14)

从excel中读取数据 # -*- coding: utf-8 -*- # @Time : 2020/2/12 17:23 # @File : do_excel_openpyxl_14.py # @Author: Hero Liu from openpyxl import load_workbook from day_20200208_mooc.tools.do_json_13 import DoJson import json class DoExcel: def get_data(self,

MOOC(7)- case依赖、读取json配置文件进行多个接口请求-模拟接口响应数据(18)

这里是把传入的请求数据作为响应值返回 # -*- coding: utf-8 -*- # @Time : 2020/2/15 9:47 # @File : do_mock_18.py # @Author: Hero Liu # 接口不可用,模拟返回响应数据 import mock def mock_test(mock_method, url, method, request_data, response_data, header=None): mock_method = mock.Mock(re

MOOC(7)- case依赖、读取json配置文件进行多个接口请求-执行测试用例(16)

执行测试用例 # -*- coding: utf-8 -*- # @Time : 2020/2/12 22:56 # @File : run_test_16.py # @Author: Hero Liu import sys sys.path.append("h:/python_lemon") import unittest from day_20200208_mooc.base.test_class_15 import TestRequest import HTMLTestRunne

MOOC(7)- case依赖、读取json配置文件进行多个接口请求-xlrd操作excel(11)

xlrd操作excel # -*- coding: utf-8 -*- # @Time : 2020/2/12 9:14 # @File : do_excel_xlrd_11.py # @Author: Hero Liu import xlrd # 打开工作簿 work_book = xlrd.open_workbook("../data/test_data.xlsx") # 通过指定索引获取sheet表,索引从0开始:也可以通过指定表名来获取 # table = work_book.

.net core读取json格式的配置文件

在.Net Framework中,配置文件一般采用的是XML格式的,.NET Framework提供了专门的ConfigurationManager来读取配置文件的内容,.net core中推荐使用json格式的配置文件,那么在.net core中该如何读取json文件呢? 1.在Startup类中读取json配置文件 1.使用Configuration直接读取 看下面的代码: public IConfiguration Configuration { get; } Configuration属