Search code examples
c#asp.net-mvctuplesjson-deserialization

C# MVC can't Deserialize a tuple


I have a Json model that looks like this

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; }
 }

And I try to de-serialize the object like this

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

The data that I'm trying to de-serialize (aka "CommentAsString") looks like this

"{\"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\"}}"

But I keep getting this error
enter image description here

Does anyone see what the problem is?

Update
The integers in CommentAsString are variables and will be different every time the function is called so I can't make a Json Object that has a key value of a particular integer.


Solution

  • Let's look at the actual formatted data structure

    {
       "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"
       }
    }
    

    Converting these to an array of tuple would be unusual. What you seemingly have are dictionary's

    Example

    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; }
     }
    

    It's debatable whether the last property is an object or another dictionary as well.

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

    However, I'll leave that up to you.