gpt4 book ai didi

c# - 为 YARP 配置反序列化 JSON

转载 作者:行者123 更新时间:2023-12-05 04:21:47 31 4
gpt4 key购买 nike

以下JSON是我的请求体:

{
"Routes": {
"route1": {
"ClusterId": "cluster1",
"Match": {
"Path": "{**catch-all}",
"Hosts": ["www.aaaaa.com", "www.bbbbb.com"]
}
}
},
"Clusters": {
"cluster1": {
"Destinations": {
"cluster1/destination1": {
"Address": "https://example.com/"
}
}
}
}
}

我试图将它转换为对象,但它不起作用

var reqItems1 = JsonConvert.DeserializeObject<Req>(jsonData);

Req 是:

public class Req
{
public RouteConfig Routes { get; set; }
public ClusterConfig Clusters { get; set; }
}

RouteConfigClusterConfig 定义:

https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuration.RouteConfig.html

https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuration.ClusterConfig.html

一切都是空的。如何将请求直接解析为对象?

最佳答案

从附加的 JSON 中,RoutesClusters 属性是键值对。

因此你的 Req 类应该如下所示:

public class Req
{
public Dictionary<string, RouteConfig> Routes { get; set; }
public Dictionary<string, ClusterConfig> Clusters { get; set; }
}

关于c# - 为 YARP 配置反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74150754/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com