我从Web请求中获得了一个JSON:
var client = new HttpClient();
var json = await client.GetFromJsonAsync<dynamic>(url);
我得到的JSON如下所示(我无法控制它,因此无法更改它):
{
"1": {
"2023-03-10": [
{
"label": "AAA",
"prop1": 123,
"prop2": 456,
},
{
"label": "BBB",
"prop1": 123,
"prop2": 456,
},
{
"label": "CCC",
"prop1": 123,
"prop2": 456,
}
],
"2023-03-11": [
{
"label": "CCC",
"prop1": 123,
"prop2": 456,
},
{
"label": "DDD",
"prop1": 123,
"prop2": 456,
}
]
},
"2": {
"2023-03-10": [...],
"2023-03-11": [...]
},
...
...
}
请注意,"1"、"2"和日期是一个数组中的100个元素.
我怎么才能用这个工作呢?
我能以某种方式在它上面循环吗?
foreach(var item in json?.somethingGetAllProperties()?)
{
var id = item?.what?
}
或者我能把它转换成我能用的东西吗?
var client = new HttpClient();
var json = await client.GetFromJsonAsync<MyObject>(url, new JsonSerializerOptions(){ ?what? });
[?SomethingHere?]
class MyObject
{
[?OrHere?]
public int Id { get; set; }
public List<Foo> Foo { get; set; }
}
class Foo
{
public DateTime Date { get; set; }
public List<Baa> Baa{ get; set; }
}
class Baa
{
public string label{ get; set; }
public int prop1 { get; set; }
public int prop2 { get; set; }
}
我以前只使用过简单的json转换.比如将属性的名称更改为新的名称.