Skip to main content

Convert List Object to JSON in C#

Conversion of object to Json is pretty simple, can be easily achieved by adding Newtonsoft JSON.NET dll to the solution. While the object is list follow the below code to convert to the pure JSON format.

Wrapper Class:

    public class ColorDeleteOutput
    {
        public List<ResponseMessage> Response { get; set; }
    }

    public class ResponseMessage
    {
        public string StyleNumber { get; set; }
        public string ColorLongDesc { get; set; }
        public string DistroDesc { get; set; }
        public string Status { get; set; }
    }



Adding value to Objects:

List<ResponseMessage> respmsg = new List<ResponseMessage>();

respmsg.Add(new ResponseMessage { ColorLongDesc = "BLUE", StyleNumber = "124578", DistroDesc = "MARIO BRACKEN", Status = "SUCCESS" });



Converting to JSON:

  string sJSONResponse = JsonConvert.SerializeObject(respmsg);


[ Note : Make sure that you have added Newtonsoft Json.Net dll and Add its namespace as using Newtonsoft.Json; ]


Comments

  1. you can install newtonsoft

    https://www.nuget.org/packages/Newtonsoft.Json/

    pkg manager command: Install-Package Newtonsoft.Json -Version 12.0.1
    .net cli command: dotnet add package Newtonsoft.Json --version 12.0.1
    packet cli command: paket add Newtonsoft.Json --version 12.0.1

    i use : dotnet add package Newtonsoft.Json --version 12.0.1

    ReplyDelete

Post a Comment