【问题标题】:JsonConvert.DeserializeObject does not deserialize my jsonJsonConvert.DeserializeObject 不反序列化我的 json
【发布时间】:2020-03-29 16:16:00
【问题描述】:

我有很多课,其中一门如下

public class DeletePatientCommand
{
    public int Id { get; set; }
}

我进行了以下 WCF 操作:

object command = JsonConvert.DeserializeObject(
    "{\"DeletePatientCommand\":{\"Id\":5}}",
    typeof(DeletePatientCommand));

对象已反序列化,但其Id 始终为0 而不是5

【问题讨论】:

  • 你有没有试过调试代码,因为我很确定Assembly.GetType会抛出异常,因为you didn't specify the full name of the type
  • 嗨 Steven,我没有故意指定全名,因为我将我的类放在默认命名空间中。
  • 请包含异常及其所有内部异常的完整堆栈跟踪。
  • 没有抛出异常!
  • 您是否尝试过发布{"Id":5} 而不是{"DeletePatientCommand":{"Id":5}}

标签: c# json.net


【解决方案1】:

作为Steven cmets 先生,我发布的 JSON 无效。 JSON 不包含类型名称。以下作品:

object command = JsonConvert.DeserializeObject(
    "{\"Id\":5}",
    typeof(DeletePatientCommand));

聊天也包含有用的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2020-12-04
    相关资源
    最近更新 更多