我从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转换.比如将属性的名称更改为新的名称.

推荐答案

最简单的方法是使用词典

var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();

var data = JsonConvert.DeserializeObject<Dictionary<string, 
                               Dictionary<string,List<Props>>>>(json);

string label=data["1"]["2023-03-11"][0].label; //"CCC"

//or 
List<Props> props20230311 = data["1"]["2023-03-11"].Select(x => x).ToList();

public class Props
{
    public string label { get; set; }
    public int prop1 { get; set; }
    public int prop2 { get; set; }
}

或者,您可以使用严格类型的列表,但它要复杂得多

List<CountDateProps> props = JObject.Parse(json).Properties()
    .Select(jo => new CountDateProps
    {
        Count = jo.Name,
        DateProps = ((JObject)jo.Value).Properties().Select(v => new DateProps
        {
            Date = Convert.ToDateTime(v.Name),
            Props = v.Value.ToObject<List<Props>>()
        }).ToList()
        }).ToList();
        
List<Props> props20230310 = props.Where(p=> p.Count=="1").First()
                                .DateProps.Where(v=> v.Date== new DateTime(2023,03,11))
                                .FirstOrDefault().Props;    
    
public class CountDateProps
{
    public string Count {get;set;}
    public List<DateProps> DateProps { get; set; }
    
}
public class DateProps
{
    public DateTime Date { get; set; }
    public List<Props> Props {get; set;}
}

Csharp相关问答推荐

C#中的两个列表中的元素加/减/乘/除操作

由于小数,如何将一个数字四舍五入到下一个数字?

将实例绑定方法传递到列表foreach

C#将参数传递给具有变化引用的变量

使用C#中的Shape API从Azure目录获取所有用户

EF Core 8—应用客户端投影后无法转换集操作

如何保持主摄像头视角保持一致?

HttpConext.Request.Path和HttpConext.GetEndpoint()之间的差异

最新的Mediatr和具有同步方法的处理程序Handle:并非所有代码路径都返回值"

在路由中使用枚举

如何使用EF Core和.NET 8来upsert到具有多对多关系的表?

Docker Container中的HttpRequest后地址不可用

NET8 MAUI并部署到真实设备上进行测试

如何将MemberInitExpression添加到绑定中其他Lambda MemberInitExpression

Lambda表达式如何与隐式强制转换一起工作?

在C#中过滤Excel文件

在使用StringBuilder时,如何根据 colored颜色 设置为richTextBox中的特定行着色?

在同一个捕获中可以有多种类型的异常吗?

WPF如何获取有关从一个视图模型更改另一个视图模型的信息

如果所有";async任务方法()";调用都返回Task.FromResult()-是否同步执行?