Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

asp.net mvc - 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":"[email protected]","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":"[email protected]"}}"

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Lets look at the actual formatted data structure

{
   "entertain":"PEG",
   "master":"Phos Ent Group",
   "memail":"[email protected]",
   "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":"[email protected]"
   }
}

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

Its debatable whether the last property is an object or another dictionary as well...

"syncs":{
   "Other pubber":"[email protected]"
}

However, ill leave that up to you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...