-1

抱歉,如果之前有人问过,但我在这方面找不到太多,因此在这里问。

我需要将我的案例保存在我的 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>();
4

1 回答 1

0

使用 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"
      }
    }
于 2018-02-07T17:57:09.710 回答