步骤
- 读取配置文件转换成字符串,代码如下
string contents = System.IO.File.ReadAllText("config.json");
注意:该语句会抛出文件不存在异常。
- 使用
Newtonsoft.Json将json字符串转换成类的对象,完整代码如下所示
public class Init
{
public static InitInfo init = new InitInfo();
public Init(){
try
{
string contents = System.IO.File.ReadAllText("config.json");
//Shell.WriteLine("ini 配置文件\r\n" + JsonConvert.SerializeObject(init));
init = JsonConvert.DeserializeObject<InitInfo>(contents);
Shell.WriteLine("初始化配置文件内容\r\n\r\n" + contents + "\r\n\r\n");
}
catch (Exception)
{
throw;
}
}
}
注意事项
-
Newtonsoft.Json需要添加引用,下载地址如下所示
https://www.newtonsoft.com/json -
可以使用
JsonConvert.SerializeObject(init)语句生成配置信息,打印在控制台,C-V。
如果这篇文章对您很有帮助,不妨
----- 本文结束 ❤ 感谢您的阅读 ----
上一篇
JAVA 环境变量配置
- 本文作者: Memory
- 本文链接: https://www.mzpkz.top/post/jahwjoh4-/
- 版权声明: 本博客所有文章除特别声明外,转载请注明出处!