C# Json反序列化错误。不支持的类型:System.Guid

C# Json反序列化错误。不支持的类型:System.Guid,c#,json,deserialization,C#,Json,Deserialization,我在做一个API项目,得到了以下问题 这是我传递的Json { "MainClass": [ { "Text": ".", "Id": { "System.Guid": "06073a9c-9cef-4f07-9180-2e54e0fa6416" } } ] } 这是我的c#Models 我怎样才能修好它 当我使用postman发布json时就会发生这种情况。我可以看到它击中了控制器,但没有击中动作。它抛出如上所述的错

我在做一个API项目,得到了以下问题

这是我传递的Json

{
  "MainClass": [
    {
      "Text": ".",
      "Id": {
        "System.Guid": "06073a9c-9cef-4f07-9180-2e54e0fa6416"
      }
    }
  ]
}
这是我的c#Models

我怎样才能修好它


当我使用postman发布json时就会发生这种情况。我可以看到它击中了控制器,但没有击中动作。它抛出如上所述的错误。

您需要以下类文件

public class Id
{
    [JsonProperty("System.Guid")]
    public string System_Guid { get; set; }
}


public class MainClass
{
    public string Text { get; set; }
    public Id Id { get; set; }
}

public class rootClass
{
    public List<MainClass> MainClass { get; set; }
}
公共类Id
{
[JsonProperty(“System.Guid”)]
公共字符串系统_Guid{get;set;}
}
公共类主类
{
公共字符串文本{get;set;}
公共Id{get;set;}
}
公共类根类
{
公共列表MainClass{get;set;}
}
1)对于您的第一个json

{
  "MainClass": [
    {
      "Text": ".",
      "Id": {
        "System.Guid": "06073a9c-9cef-4f07-9180-2e54e0fa6416"
      }
    }
  ]
}
您需要再创建一个类模型来保存Guid类型的属性

public class RootClass
{
    public List<MainClass> MainClass { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public _Guid Id { get; set; }
}

public class _Guid
{
    [JsonProperty("System.Guid")]
    public Guid Id { get; set; }
}
您需要在类层次结构下正确地反序列化json

public class RootClass
{
    public List<MainClass> MainClass { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public Guid Id { get; set; }       //<= The type "System.Guid" as it is
}
公共类根类
{
公共列表MainClass{get;set;}
}
公共类主类
{
公共字符串文本{get;set;}
公共Guid Id{get;set;}//Console.WriteLine(“Text:\t”+x.Text+“\n”+“Id:\t”+x.Id));


输出:(用于上述两种json)

我通常使用这样的对象

所以,结果是

public class Id
{
    public string MyGuid { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public Id Id { get; set; }
}

public class RootObject
{
    public List<MainClass> MainClass { get; set; }
}
公共类Id
{
公共字符串MyGuid{get;set;}
}
公共类主类
{
公共字符串文本{get;set;}
公共Id{get;set;}
}
公共类根对象
{
公共列表MainClass{get;set;}
}
不能使用System.Guid名称作为属性名称

重要:像这样反序列化对象

RootObject myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json.Replace("System.Guid","MyGuid"));
RootObject myObject=Newtonsoft.Json.JsonConvert.DeserializeObject(Json.Replace(“System.Guid”、“MyGuid”);

System.Guid不能是字符串变量,因此需要将其替换为_感谢您的输入,但表示Guid将自动映射,对吗?System.Guid不能是变量名我在下面添加了我的答案以及输出和在线演示,请尝试并让我知道:)
{
  "MainClass": [
    {
      "Text": ".",
      "Id": "06073a9c-9cef-4f07-9180-2e54e0fa6416"
    }
  ]
}
public class RootClass
{
    public List<MainClass> MainClass { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public Guid Id { get; set; }       //<= The type "System.Guid" as it is
}
RootClass mainClass = JsonConvert.DeserializeObject<RootClass>(json);

mainClass.MainClass.ForEach(x => Console.WriteLine("Text:\t" + x.Text + "\n" + "Id:\t" + x.Id));    
public class Id
{
    public string MyGuid { get; set; }
}

public class MainClass
{
    public string Text { get; set; }
    public Id Id { get; set; }
}

public class RootObject
{
    public List<MainClass> MainClass { get; set; }
}
RootObject myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json.Replace("System.Guid","MyGuid"));