Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 剩余有效载荷案例保存_C#_Json_Rest_Json.net_Flurl - Fatal编程技术网

C# 剩余有效载荷案例保存

C# 剩余有效载荷案例保存,c#,json,rest,json.net,flurl,C#,Json,Rest,Json.net,Flurl,如果早些时候有人问过,我表示歉意,但我找不到更多关于这一点的信息,因此在这里提问 我需要在REST请求JSON负载中保留我的案例。 我也使用了JsonProperty,但它确实有帮助 我有一个对应于JSON POST请求负载的类: namespace AugmentedAi.Common { [Serializable] public class InstallSoftwareMetadata { [JsonProperty(propertyName:"

如果早些时候有人问过,我表示歉意,但我找不到更多关于这一点的信息,因此在这里提问

我需要在REST请求JSON负载中保留我的案例。 我也使用了JsonProperty,但它确实有帮助

我有一个对应于JSON POST请求负载的类:

namespace AugmentedAi.Common
{
    [Serializable]
    public class InstallSoftwareMetadata
    {

        [JsonProperty(propertyName:"sourceType")]
        public string SourceType { get; set; }
        [JsonProperty(propertyName: "toolInstance")]
        public string ToolInstance { get; set; }


        public static InstallSoftwareMetadata GetInstallSoftwareMetadata()
        {
            var ticketDetail = new TicketDetail
            {
                Switch = "/easy",
                Number = Guid.NewGuid().ToString()
            };

            return new InstallSoftwareMetadata
            {
                SourceType = "rest1",
                ToolInstance = "rest1",
                TicketDetail = ticketDetail
            };
        }
    }
}

public class TicketDetail
    {

       [JsonProperty("number")]
        public string Number { get; set; }

        [JsonProperty("Switch")]
        public string Switch { get; set; }

    }
由于案例不匹配,我从服务器收到一个异常。 在异常消息中,我可以看到我的所有请求参数都已序列化为小写

这里有什么可以帮忙的?请建议

编辑: 这就是我发送REST请求的方式。PostJsonAsync正在序列化它

var installSoftwarResponse = await baseUrl.WithHeader("Accept", "application/json")
                    .WithHeader("Content-Type", "application/json")                        .PostJsonAsync(InstallSoftwareMetadata.GetInstallSoftwareMetadata())
                    .ReceiveJson<InstallSoftwareResponse>();
var installSoftwarResponse=wait baseUrl.WithHeader(“接受”、“应用程序/json”)
.WithHeader(“内容类型”、“应用程序/json”).PostJsonAsync(InstallSoftwareMatadata.GetInstallSoftwareMatadata())
.ReceiveJson();

使用JSON.NET,您可以这样做,并注意不需要[Serializable]属性

void Main()
{
    var res = JsonConvert.SerializeObject(InstallSoftwareMetadata.GetInstallSoftwareMetadata());
}

public class InstallSoftwareMetadata
{

    [JsonProperty("sourceType")]
    public string SourceType { get; set; }
    [JsonProperty("toolInstance")]
    public string ToolInstance { get; set; }
    [JsonProperty("ticketDetail")]
    public TicketDetail TicketDetail { get; set; }

    public static InstallSoftwareMetadata GetInstallSoftwareMetadata()
    {
        var ticketDetail = new TicketDetail
        {
            Switch = "/easy",
            Number = Guid.NewGuid().ToString()
        };

        return new InstallSoftwareMetadata
        {
            SourceType = "rest1",
            ToolInstance = "rest1",
            TicketDetail = ticketDetail
        };
    }
}

public class TicketDetail
{

    [JsonProperty("number")]
    public string Number { get; set; }

    [JsonProperty("Switch")]
    public string Switch { get; set; }

}
结果

 {
      "sourceType": "rest1",
      "toolInstance": "rest1",
      "ticketDetail": {
        "number": "2c8b5d48-315e-406c-ad4f-b07922fdd135",
        "Switch": "/easy"
      }
    }

您使用什么代码将其转换为JSON。看起来您正在混合使用JSON.NET和内置JSON序列化程序。您可以分享一个演示您的问题的示例吗?您展示了您的模型,但没有展示它们是如何序列化的,甚至没有展示您正在使用的框架。