skip to Main Content

can you please help me. I need to create json from two different clases
Here what I have:

public class SmtpData
{
    public string id { get; set; }
    public string settingName { get; set; }
    public string host { get; set; }
    public int port { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string senderName { get; set; }
    public string senderEmail { get; set; }
    public string senderMessageIdDomain { get; set; }
    public string senderReplyTo { get; set; }
    public string senderListUnsubscribe { get; set; }
    public bool isSecure { get; set; }
    public bool isUseProxy { get; set; }
    public class Proxy
    {
        public string proxyhost { get; set; }
        public int proxyport { get; set; }
        public string proxyusername { get; set; }
        public string proxypassword { get; set; }

    }

And from this, I create a json file with my configs

        public void SaveSmtp(SmtpData data)
        {
            var dataJson = JsonConvert.SerializeObject(data);
            string resultPath = Path.Combine(Helpers.SmtpPath, $"{data.id}.conf");
            File.WriteAllText(resultPath, dataJson);
        }

I need to have configs in this file, not only from SmtpData, but with SmtpData.Proxy in the same string

Here is what I have:
{"id":"YXe1yCcNlc","settingName":"AAAA","host":"AAAA","port":0,"username":"AAAA","password":"AAAA","senderName":"AAAA","senderEmail":"AAAA","senderMessageIdDomain":"AAAA","senderReplyTo":"AAAA","senderListUnsubscribe":"AAAA","isSecure":true,"isUseProxy":false}

Here is what I need:
{"id":"YXe1yCcNlc","settingName":"AAAA","host":"AAAA","port":0,"username":"AAAA","password":"AAAA","senderName":"AAAA","senderEmail":"AAAA","senderMessageIdDomain":"AAAA","senderReplyTo":"AAAA","senderListUnsubscribe":"AAAA","isSecure":true,"isUseProxy":false,"proxyhost":"AAAA","proxyport":0,"proxyusername":"AAAA","proxypassword":"AAAA"}

Thank you very much my friends.

I have readed many faq and guids … but I didnt find any solution …

2

Answers


  1. In this case you need change result string with Json value – insert serialized SmtpData.Proxy into serialized SmtpData. Or using reflection and JObject

    Because you class Proxy not a part of SmtpData class, if you add a
    Proxy property – {"id":"YXe1yCcNlc","settingName":"AAAA","host":"AAAA","port":0,"username":"AAAA","password":"AAAA","senderName":"AAAA","senderEmail":"AAAA","senderMessageIdDomain":"AAAA","senderReplyTo":"AAAA","senderListUnsubscribe":"AAAA","isSecure":true,"isUseProxy":false, "proxy":{ "proxyhost":"AAAA","proxyport":0,"proxyusername":"AAAA","proxypassword":"AAAA"}}

    Login or Signup to reply.
  2. Your Class declaration isn’t neat, and that is confusing you.

    public class SmtpData
        {
            public string id { get; set; }
            public string settingName { get; set; }
            public string host { get; set; }
            public int port { get; set; }
            public string username { get; set; }
            public string password { get; set; }
            public string senderName { get; set; }
            public string senderEmail { get; set; }
            public string senderMessageIdDomain { get; set; }
            public string senderReplyTo { get; set; }
            public string senderListUnsubscribe { get; set; }
            public bool isSecure { get; set; }
            public bool isUseProxy { get; set; }
    
            public Proxy proxy { get; set; }
            
        }
    
        public class Proxy
        {
            public string proxyhost { get; set; }
            public int proxyport { get; set; }
            public string proxyusername { get; set; }
            public string proxypassword { get; set; }
    
        }
    

    Define your classes as such, and try to deserialize again. Should give you, your expected outcome.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search