如何从C#应用程序设置读取Json对象

问题描述

我有一个json格式的kubernetes配置文件,我想使用C#kubernetes clinet api部署到kubernetes集群中。以下是我的配置数据文件。当我尝试使用 Microsoft.Extensions.Configuration 从appsetting.json读取值时,值始终为null。以下是yaml文件

{
"yaml":{
    "apiVersion": "batch/v1beta1","kind": "CronJob","metadata": {
        "creationTimestamp": "2020-08-04T06:29:19Z","name": "esignature-cron","namespace": "esignature"
    },"spec": {
        "concurrencyPolicy": "Forbid","failedJobsHistoryLimit": 1,"jobTemplate": {
            "metadata": {
                "creationTimestamp": null
            },"spec": {
                "template": {
                    "metadata": {
                        "creationTimestamp": null,"labels": {
                            "app": "your-periodic-batch-job"
                        }
                    },"spec": {
                        "containers": [
                            {
                                "image": "pystore.azurecr.io/py-app1:1.0.0","imagePullPolicy": "IfNotPresent","name": "redmine-cron","resources": {},"terminationMessagePath": "/dev/termination-log","terminationMessagePolicy": "File"
                            }
                        ],"dnsPolicy": "ClusterFirst","imagePullSecrets": [
                            {
                                "name": "dockerpull"
                            }
                        ],"restartPolicy": "OnFailure","schedulerName": "default-scheduler","securityContext": {},"terminationGracePeriodSeconds": 30
                    }
                }
            }
        },"schedule": "*/1 * * * *","successfulJobsHistoryLimit": 3,"suspend": false
    }
}
"ConnectionString":"test"
}

C#代码

using Microsoft.Extensions.Configuration;
namespace yamlread.Services.Implementation
{
public class yamlread : Iyamlread
    {
 public IConfiguration Configuration { get; }
 public string yaml { get; set;}
 public yamlread( IConfiguration config)
 {
     Configuration =config;
     yaml=Configuration["yaml"]
 }

解决方法

GitHub中很少有可用于将Yaml解析为c#对象的库。

下面是链接

https://github.com/aaubry/YamlDotNet

https://github.com/xoofx/SharpYaml

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...