JsonConvert.DeserializeObject不会反序列化我的json

时间:2019-12-04 19:12:12

标签: c# json.net

我有很多课,其中一类是以下

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

我进行了以下WCF操作:

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

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

1 个答案:

答案 0 :(得分:0)

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

class Element {
  public:
    Element() {};
    Element(std::string s, int a) { name = s; value = a; };
    Element(std::string s) { name = s; value = 0; };
    ...
    std::string return_name() { return name; };
  private:
    std::string name;
    int value;
 };

std::string fun(Element x) { return x.return_name(); };

聊天还包含有用的信息。