Creating JSON from C# Class

Kmcnet 1,066 Reputation points
2024-01-04T03:18:53.9366667+00:00

Hello everyone and thanks for the help in advance. I need to create a JSON file from a C# however I have never done so and can't find a tutorial that works for me. A partial look at the JSON:

  "Message": {     "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",     "@DatatypesVersion": "20170715",     "@TransportVersion": "20170715",     "@TransactionDomain": "SCRIPT",     "@TransactionVersion": "20170715",     "@StructuresVersion": "20170715",     "@ECLVersion": "20170715",     "Header": {       "To": {         "@Qualifier": "P",         "#text": "7654321"       }

I created a class, called DeserializedClass that contains multiple classes beginning with

public class Root {     public Message Message { get; set; } }

Class Message:

        public class Message         {             [JsonProperty("@DatatypesVersion")]             public string DatatypesVersion { get; set; }              [JsonProperty("@TransportVersion")]             public string TransportVersion { get; set; }              [JsonProperty("@TransactionDomain")]             public string TransactionDomain { get; set; }              [JsonProperty("@TransactionVersion")]             public string TransactionVersion { get; set; }              [JsonProperty("@StructuresVersion")]             public string StructuresVersion { get; set; }              [JsonProperty("@ECLVersion")]             public string ECLVersion { get; set; }             public Header Header { get; set; }             public Body Body { get; set; }         }

And another class Header:

        public class Header         {             public To To { get; set; }             public From From { get; set; }             public string MessageID { get; set; }             public DateTime SentTime { get; set; }             public Security Security { get; set; }             public SenderSoftware SenderSoftware { get; set; }             public DigitalSignature DigitalSignature { get; set; }         }

But I don't know what to do from here, especially the nesting of sub-elements, i.e. Header is nested within Message. Any help would be appreciated.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-01-04T06:40:05.36+00:00

    Hi @Kmcnet , Welcome to Microsoft Q&A,

    Do you want this effect?

    User's image

    First, make sure to install the Newtonsoft.Json NuGet package if you haven't already.

    The JsonProperty attribute is used to specify the JSON property names, and the structure follows the desired format. You can customize the class structure based on the specific requirements of your JSON structure.

    using Newtonsoft.Json;
    using System;
    
    namespace xxx
    {
        public class To
        {
            [JsonProperty("@Qualifier")]
            public string Qualifier { get; set; }
    
            [JsonProperty("#text")]
            public string Text { get; set; }
        }
        public class Header
        {
            public To To { get; set; }
        }
    
        public class Message
        {
            [JsonProperty("@xmlns:xsi")]
            public string XmlnsXsi { get; set; }
    
            [JsonProperty("@DatatypesVersion")]
            public string DatatypesVersion { get; set; }
    
            [JsonProperty("@TransportVersion")]
            public string TransportVersion { get; set; }
    
            [JsonProperty("@TransactionDomain")]
            public string TransactionDomain { get; set; }
    
            [JsonProperty("@TransactionVersion")]
            public string TransactionVersion { get; set; }
    
            [JsonProperty("@StructuresVersion")]
            public string StructuresVersion { get; set; }
    
            [JsonProperty("@ECLVersion")]
            public string ECLVersion { get; set; }
    
            public Header Header { get; set; }
        }
    
        internal class Program
        {
            static void Main(string[] args)
            {
                // Create an instance of the Message class with sample data
                Message message = new Message
                {
                    XmlnsXsi = "http://www.w3.org/2001/XMLSchema-instance",
                    DatatypesVersion = "20170715",
                    TransportVersion = "20170715",
                    TransactionDomain = "SCRIPT",
                    TransactionVersion = "20170715",
                    StructuresVersion = "20170715",
                    ECLVersion = "20170715",
                    Header = new Header
                    {
                        To = new To
                        {
                            Qualifier = "P",
                            Text = "7654321"
                        }
                    }
                };
    
                // Convert the Message instance to JSON
                string json = JsonConvert.SerializeObject(message);
    
                // Display the generated JSON
                Console.WriteLine(json);
                Console.ReadLine();
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2024-01-04T06:38:07.2933333+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.