如何将appsettings.json部分转换为动态对象,然后在C#中转换为类对象?

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

How to convert appsettings.json section to dynamic and then to class object in C#

问题

我将以下部分进行翻译:

我有一个 appsettings:

"testData": [
    {
      "target": "MarketMirrorClient.GetContests",
      "mockData": [
        {
          "request": {
            "contestKeys": [ "testContestKey1" ]
          },
          "response": {
            "contests": [
              {
                "contestKey": "testContestKey1",
                "contestType": "football",
                "contestStatus": "InPlay"
              }
            ]
          }
        },
        {
          "request": {
            "contestKeys": [ "testContestKey2" ]
          },
          "response": {
            "contests": [
              {
                "contestKey": "testContestKey2",
                "contestType": "soccer",
                "contestStatus": "PreGame"
              }
            ]
          }
        }
      ]
    }
  ]

我将只回答你的问题,不提供额外内容。关于在哪里实现 IConvertible,你可以在 MockData 类的 Response 属性上实现 IConvertible 接口。在你的情况下,这是因为你尝试将 ResponseDictionary<string, object> 转换为特定类型时出现异常,所以你需要在 Response 类型上实现 IConvertible 接口以进行类型转换。

英文:

I have an appsettings:

&quot;testData&quot;: [
    {
      &quot;target&quot;: &quot;MarketMirrorClient.GetContests&quot;,
      &quot;mockData&quot;: [
        {
          &quot;request&quot;: {
            &quot;contestKeys&quot;: [ &quot;testContestKey1&quot; ]
          },
          &quot;response&quot;: {
            &quot;contests&quot;: [
              {
                &quot;contestKey&quot;: &quot;testContestKey1&quot;,
                &quot;contestType&quot;: &quot;football&quot;,
                &quot;contestStatus&quot;: &quot;InPlay&quot;
              }
            ]
          }
        },
        {
          &quot;request&quot;: {
            &quot;contestKeys&quot;: [ &quot;testContestKey2&quot; ]
          },
          &quot;response&quot;: {
            &quot;contests&quot;: [
              {
                &quot;contestKey&quot;: &quot;testContestKey2&quot;,
                &quot;contestType&quot;: &quot;soccer&quot;,
                &quot;contestStatus&quot;: &quot;PreGame&quot;
              }
            ]
          }
        }
      ]
    }
  ]

I serialize the testData.mockData.request to a Dictionary&lt;string, object&gt;. However, when I try to convert the retrieved object from the dictionary to a particular type, it throws an exception saying IConvertible needs to be implemented e.g.

builder.Services.Configure&lt;MockTestData&gt;(&quot;testData&quot;);

The models are:

public class MockTestData
{
    public List&lt;TestData&gt; TestData { get; set; } = new();
}

public class TestData
{
    public string Target { get; set; }
    public List&lt;MockData&gt; MockData { get; set; } = new();
}

public class MockData
{
    public Dictionary&lt;string, object&gt; Request { get; set; }
    public object Response { get; set; }
}

Where should I implement the IConvertible?

I tried changing the types from object to dynamic or ExpandoObject but the error is either IConvertible needs to be implemented or Cannot convert object to 'whatever' type i am casting to i.e. List<string>

答案1

得分: 2

这对我有效

List<TestDatum> testData = configuration.GetSection("testData")
                                        .Get<List<TestDatum>>();

public class TestDatum
{
	public string target { get; set; }
	public List<MockDatum> mockData { get; set; }
}

public class Contest
{
	public string contestKey { get; set; }
	public string contestType { get; set; }
	public string contestStatus { get; set; }
}

public class MockDatum
{
	public Request request { get; set; }
	public Response response { get; set; }
}

public class Request
{
	public List<string> contestKeys { get; set; }
}

public class Response
{
	public List<Contest> contests { get; set; }
}
```

<details>
<summary>英文:</summary>

this works for me

List<TestDatum> testData = configuration.GetSection("testData")
.Get<List<TestDatum>>();

public class TestDatum
{
public string target { get; set; }
public List<MockDatum> mockData { get; set; }
}

public class Contest
{
public string contestKey { get; set; }
public string contestType { get; set; }
public string contestStatus { get; set; }
}

public class MockDatum
{
public Request request { get; set; }
public Response response { get; set; }
}

public class Request
{
public List<string> contestKeys { get; set; }
}

public class Response
{
public List<Contest> contests { get; set; }
}

huangapple
  • 本文由 发表于 2023年8月4日 21:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定