英文:
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
接口。在你的情况下,这是因为你尝试将 Response
从 Dictionary<string, object>
转换为特定类型时出现异常,所以你需要在 Response
类型上实现 IConvertible
接口以进行类型转换。
英文:
I have an 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"
}
]
}
}
]
}
]
I serialize the testData.mockData.request
to a Dictionary<string, object>
. 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<MockTestData>("testData");
The models are:
public class MockTestData
{
public List<TestData> TestData { get; set; } = new();
}
public class TestData
{
public string Target { get; set; }
public List<MockData> MockData { get; set; } = new();
}
public class MockData
{
public Dictionary<string, object> 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; }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论