C# MVC 无法反序列化元组

时间:2021-01-23 01:53:18

标签: c# asp.net-mvc tuples json-deserialization

我有一个像这样的 Json 模型

private class SearchMetadataJson
{
      public string entertain { get; set; }
      public string master { get; set; }
      public string memail { get; set; }
      public string key { get; set; }
      public (int, string)[] mood { get; set; }
      public int? soundnumber { get; set; }
      public int? ftv { get; set; }
      public int? com { get; set; }
      public (int, string)[] sims { get; set; }
      public (int, string)[] keysecond { get; set; }
      public string popt { get; set; }
      public (string, string) syncs { get; set; }
 }

我尝试像这样反序列化对象

var CommentObj = JsonSerializer.Deserialize<SearchMetadataJson>(CommentAsString);

我试图反序列化的数据(又名“CommentAsString”)看起来像这样

"{\"entertain\":\"PEG\",\"master\":\"Phos Ent Group\",\"memail\":\"example@example.com\",\"key\":\"Db\",\"mood\":{\"1\":\"TypeA\",\"4\":\"TypeB\",\"5\":\"TypeC\"},\"soundnumber\":\"5\",\"ftv\":\"4\",\"com\":\"3\",\"sims\":{\"1\":\"Band1\",\"2\":\"Band2\"},\"keysecond\":{\"1\":\"KeyWord1\",\"2\":\"KeyWord2\",\"3\":\"KeyWord3\"},\"syncs\":{\"Other pubber\":\"example2@example.com\"}}"

但是我一直收到这个错误
enter image description here

有人看到问题出在哪里了吗?

更新
CommentAsString 中的整数是变量,每次调用函数时都会有所不同,因此我无法创建具有特定整数键值的 Json 对象。

3 个答案:

答案 0 :(得分:2)

我们来看看实际的格式化数据结构

{
   "entertain":"PEG",
   "master":"Phos Ent Group",
   "memail":"example@example.com",
   "key":"Db",
   "mood":{
      "1":"TypeA",
      "4":"TypeB",
      "5":"TypeC"
   },
   "soundnumber":"5",
   "ftv":"4",
   "com":"3",
   "sims":{
      "1":"Band1",
      "2":"Band2"
   },
   "keysecond":{
      "1":"KeyWord1",
      "2":"KeyWord2",
      "3":"KeyWord3"
   },
   "syncs":{
      "Other pubber":"example2@example.com"
   }
}

将这些转换为元组数组是不寻常的。你看似拥有的是字典的

示例

private class SearchMetadataJson
{
      public string entertain { get; set; }
      public string master { get; set; }
      public string memail { get; set; }
      public string key { get; set; }
      public Dictionary<int,string> mood { get; set; }
      public int? soundnumber { get; set; }
      public int? ftv { get; set; }
      public int? com { get; set; }
      public Dictionary<int,string> sims { get; set; }
      public Dictionary<int,string> keysecond { get; set; }
      public string popt { get; set; }
     // public (string, string) syncs { get; set; }
 }

最后一个属性是一个对象还是另一个字典是有争议的。

"syncs":{
   "Other pubber":"example2@example.com"
}

不过,我会把它留给你。

答案 1 :(得分:1)

您的模型有错误,请使用此站点在 C# 中转换您的 json https://json2csharp.com/

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class Mood    {
        public string _1 { get; set; } 
        public string _4 { get; set; } 
        public string _5 { get; set; } 
    }

    public class Sims    {
        public string _1 { get; set; } 
        public string _2 { get; set; } 
    }

    public class Keysecond    {
        public string _1 { get; set; } 
        public string _2 { get; set; } 
        public string _3 { get; set; } 
    }

    public class Syncs    {
        public string Otherpubber { get; set; } 
    }

    public class Root    {
        public string entertain { get; set; } 
        public string master { get; set; } 
        public string memail { get; set; } 
        public string key { get; set; } 
        public Mood mood { get; set; } 
        public string soundnumber { get; set; } 
        public string ftv { get; set; } 
        public string com { get; set; } 
        public Sims sims { get; set; } 
        public Keysecond keysecond { get; set; } 
        public Syncs syncs { get; set; } 
    }

使用这个再试一次

首先读取你的字符串 这是使用 post、get 或 delete 的响应

var response = await client.PostAsync("your-url", datasBody);
var contentData = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var CommentObj = JsonSerializer.Deserialize<Root>(contentData, options);

如果您的模型不好或不匹配使用

    [JsonProperty("entertain")]
    public string entertain { get; set; } 

答案 2 :(得分:1)

您将需要使用 custom converter 或将您的元组转换为带有字段的单独类,以解释每个字段的用途。

相关问题