-2

i'm using this class :

 class message
    {
        public content Content { get; set; }
        public from From { get; set;  }
        public personalizations Personalizations { get; set; }
    }


    public class content
    {
        public string type = "text/html";
        public string value = "html";
    }

    public class from
    {
        public string email = "example@example.com";
        public string name = "example";

    }
    public class personalizations
    {
        public List<to> tos { get; set; }
    }
    public class to
    {
        public string subject { get; set; }
        public string email { get; set; }
    }

i'm serializing the class Message to :

    var msg = new message() { Content = new content() { type = "text/html", value = "html" },
        From = new from() { email = "example@example.com", name = "example" },
        Personalizations = new personalizations() { tos = new List<to>() { new to(), new to() } } };
    var data = JsonConvert.SerializeObject(msg);

i'm trying to get an array of every parent the json output format is

{
  "Content": {

     "type": "text/html",
    "value": "html"
  },
  "From": {
    "email": "example@example.com",
    "name": "example"
   },
  "Personalizations": 
   {
    "tos": [
      {
        "subject": null,
        "email": null
      },
      {
        "subject": null,
        "email": null
      }
    ]
  }
}

but i do want this format instead :

{
  "content": [
    {
      "type": "text/html", 
      "value": "Html"
    }
  ], 
  "from": {
    "email": "", 
    "name": ""
  }, 

  "personalizations": [
    {
      "subject": "",
      "to": [ { "email": "" }]
    },
    {
        "subject": "",
        "to": [{ "email": "" }]
    },
    {
        "subject": "",
        "to": [{ "email": "" }]
    }

    ]

}

how can i manage to do change the format to the last one ?

thanks in advance

EDIT :

i want to change the format not the values example :

in the last json example i have a object of personalization wich hold multiple json but in the first one i only have an object

Olivier Oli
  • 1
  • 1
  • 1
  • 3
  • `null != ""`.... – Gusman May 05 '17 at 02:04
  • See http://stackoverflow.com/questions/23830206/json-convert-empty-string-instead-of-null – shurik May 05 '17 at 02:07
  • @shurik you are right... Obviously OP knows how to construct classes from JSON, and how to represent object as single element array - reopened and waiting for clarification on what they have problem with exactly. – Alexei Levenkov May 05 '17 at 02:15
  • Is it the NULL he's trying to handle, or is it the structure of the JSON itself? "Personalizations" has a different structure in the two examples. – Robert Paulsen May 05 '17 at 02:22
  • @shurik no i'm not talking about the values but about the format – Olivier Oli May 05 '17 at 02:29
  • @RobertPaulsen no , its not about the values , i'm just trying to make the same json format from the first one to the last one – Olivier Oli May 05 '17 at 02:30
  • Did you try to change your object composition, so it is represented as you expect. ie: list of content instead of content? – Orifjon May 05 '17 at 02:44

2 Answers2

2

You can take the JSON you want to end up with and copy it to your clipboard. You can then go to any .cs file in Visual Studio from the Edit menu you can expand the "Paste Special" menu. Choose the option "Paste JSON as classes" and you get this:

public class Rootobject
{
    public Content[] content { get; set; }
    public From from { get; set; }
    public Personalization[] personalizations { get; set; }
}

public class From
{
    public string email { get; set; }
    public string name { get; set; }
}

public class Content
{
    public string type { get; set; }
    public string value { get; set; }
}

public class Personalization
{
    public string subject { get; set; }
    public To[] to { get; set; }
}

public class To
{
    public string email { get; set; }
}
Robert Paulsen
  • 4,935
  • 3
  • 21
  • 27
0

According to your latest comment, you want to change the output format. You can achieve that by moving the property "public string subject" from the "to" class to "personalizations" class, like here:

https://dotnetfiddle.net/40nBnl

You should, by the way, take a look at C# naming conventions

J.N.
  • 537
  • 2
  • 10