Avatar of ITsolutionWizard
ITsolutionWizard
Flag for United States of America asked on

json and c#

I receive the following alert: Could not determine JSON object type for type System.Collections.Generic.Dictionary`2[System.String, System.String].
do you know why and how to fix it?


I expect to set the result:
{
  "action": "cancel",
  "registrants": [
    {
      "id": "1222",
      "email": "hello@gmail.com"
    }
  ]
}

            var model1 = new Dictionary<string, string>();
            model1.Add("id", "1222");
            model1.Add("email", "hello@gmail.com");

            dynamic test = new JObject();
            test.action = "Cancel";
            test.registrants = model1;

            Response.Write(test.ToString());

Open in new window

ASP.NETC#JSON

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
ITsolutionWizard

ASKER
anyone can help?
ASKER CERTIFIED SOLUTION
Kelvin McDaniel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ryan Chong

try this:

public class yourObj
    {
        [JsonProperty(Order = 1)]
        public string action { get; set; }
        [JsonProperty(Order = 2)]
        public List< Dictionary<string, string>> registrants;
    }

Open in new window

then:

var model1 = new Dictionary<string, string>();
            model1.Add("id", "1222");
            model1.Add("email", "hello@gmail.com");

            yourObj test = new yourObj();
            test.action = "Cancel";

            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
            list.Add(model1);

            test.registrants = list;
            Response.Write(JsonConvert.SerializeObject(test));

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck