【问题标题】:REST JSON payload case preservationREST JSON 有效负载案例保存
【发布时间】:2018-02-07 15:35:41
【问题描述】:

抱歉,如果之前有人问过,但我找不到太多关于此的信息,因此在这里问。

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

【问题讨论】:

  • 您用来将其转换为 JSON 的代码是什么。看起来您正在混合 JSON.NET 和 Json 序列化程序中的构建
  • 你能edit 分享一个minimal reproducible example 来证明你的问题吗?您展示您的模型,但不展示它们是如何序列化的,甚至展示您正在使用的框架。

标签: c# json rest json.net flurl


【解决方案1】:

使用 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"
      }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多