0

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.

ChristianOConnor
  • 820
  • 7
  • 29

3 Answers3

2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 1
    Again, thanks so much for the help but the issue is that the numbers are changing variables so I have to define the json in the mood, sims, and keysecond as a changing integer and changing string. I tried changing them to arrays of dictionaries but then I got this error "JsonException: The JSON value could not be converted to System.Collections.Generic.Dictionary`2[System.Int32,System.String][]. Path: $.mood | LineNumber: 0 | BytePositionInLine: 111.' – ChristianOConnor Jan 23 '21 at 03:18
1

your have error in the model, use this site for convert your json in c# 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; } 
    }

try again with using this

first read your string this is the response using post, get or 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);

if your model is bad or not match using

    [JsonProperty("entertain")]
    public string entertain { get; set; } 
Daniel
  • 736
  • 4
  • 8
  • 1
    yes, right but don't forget to change property name as it does not match with jsonstring in Mood, Sims and KeySecond class otherwise value will not be binded. – Nayan Jan 23 '21 at 02:11
  • 1
    use this site, https://json2csharp.com/, convert to response json, in class in c# , When doing Deserialize, with this you can access the json data, or share your complete json response, and is it a get or post? – Daniel Jan 23 '21 at 02:23
  • This is a great post thanks. This would work but the numbers are actually also variables that will change so the models will become obsoleted after the first time the function runs. This site json2csharp.com is a great resource though thanks! – ChristianOConnor Jan 23 '21 at 03:07
1

You will either need to use a custom converter or convert your tuples into separate classes with fields to explain what each field is used for.

Coded Container
  • 863
  • 2
  • 12
  • 33
  • This looks like it could be helpful. I'm using dotnet-sdk 3.1, can you give an example of how to use a custom converter to solve this? Also, is it available in dotnet-sdk 3.1? – ChristianOConnor Jan 23 '21 at 04:05