Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how convert key value class to json

i am a class with definition below

 public class Param
    {
        public string Key { get; set; }
        public object Value { get; set; }

        public Param(string key, object value)
        {
            this.Key = key;
            this.Value = value;
        }
    }

a objects with values

var params = new List<Param>(){new Param("k1","v1"),new Param("k2","v2"),new Param("k3","v3")};

How to build a json with this structure.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
"k1":"v1",
"k2":"v2",
"k3":"v3"
}

>Solution :

var json = JsonSerializer.Serialize(params.ToDictionary(p => p.Key, p => p.Value));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading