D T
D T

Reputation: 3746

How can create JSON Object multi level?

I want using api send mail at: https://learn.microsoft.com/en-US/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations#SendMessageOnTheFly

 *POST https://outlook.office.com/api/v2.0/me/sendmail*

{
  "Message": {
    "Subject": "Meet for lunch?",
    "Body": {
      "ContentType": "Text",
      "Content": "The new cafeteria is open."
    },
    "ToRecipients": [
      {
        "EmailAddress": {
          "Address": "[email protected]"
        }
      }
    ],
    "Attachments": [
      {
        "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
        "Name": "menu.txt",
        "ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk="
      }
    ]
  },
  "SaveToSentItems": "false"
}

I try create Message Json by:

 var json=new {  "Message": { "Subject": "Meet for lunch?",.......,"SaveToSentItems": "false"};

But C# not allow.

How can create Json object Message in C#? Thank you.

Upvotes: 0

Views: 634

Answers (4)

Mahesh Nepal
Mahesh Nepal

Reputation: 1439

As @ataboo suggested the best way is to create multiple classes that match the json structure and serialize the object to JSON.

Creating all the other classes should be straight forward except for the Attachment class. C# does not allow naming properties as "public string @odata.type { get; set; }"

NewtonSoft.Json has a solution for this problem. Create your class with any legal property name for the field witht the JsonProperty attribute as follows

public class Attachment
{
    [JsonProperty("@odata.type")]
    public string OdataType { get; set; }

    public string Name { get; set; }

    public string ContentBytes { get; set; }
}

JsonProperty attribute allows you to name properties as you want regardless of the json field name. Hope it helps.

Upvotes: 3

TemaTre
TemaTre

Reputation: 1424

The problem is in

var json=new {  "Message": { "Subject": "Meet for lunch?",.......,"SaveToSentItems": "false"};

So, you can not create JSon in code. You can create dynamic object:

var json=new {  Message = new {  Subject = "Meet for lunch?",......., SaveToSentItems = false};

And then you can convert it to Json-string.

But you should understand that C# is language with strong typing. And when you call this it is not like "Json-object in JavaScript". When you write this it will create an anonimus type and compile it. So it can seems the same as Json. But it's only syntactic sugar. And problem is in strong typing. For example in Python or JavaScript it will work as dynamic object.

It's better to use method like @codemonkeytony wrote

Upvotes: 1

codemonkeytony
codemonkeytony

Reputation: 190

Your JSON should be a string

var myObject = @"{
  ""Message"": {
    ""Subject"": ""Meet for lunch?"",
    ""Body"": {
      ""ContentType"": ""Text"",
      ""Content"": ""The new cafeteria is open.""
    },
    ""ToRecipients"": [
      {
        ""EmailAddress"": {
          ""Address"": ""[email protected]""
        }
      }
    ],
    ""Attachments"": [
      {
        ""@odata.type"": ""#Microsoft.OutlookServices.FileAttachment"",
        ""Name"": ""menu.txt"",
        ""ContentBytes"": ""bWFjIGFuZCBjaGVlc2UgdG9kYXk=""
      }
    ]
  },
  ""SaveToSentItems"": ""false""
}";

//create the object
var serializer = new JavaScriptSerializer();
var result = serializer.DeserializeObject(myObject); 

//access the values

var userId = result["Message"]["Subject"];

Upvotes: 1

Tech Sourav
Tech Sourav

Reputation: 134

Copy the raw json from Fiddler, and paste it into this site: http://json2csharp.com/. It will create the C# classes you need in order to serialize back to the desired JSON format. If you are returning string and response format is set to Json, .NET automatically serializes your string. So after first serialization, .NET did another and it simply escaped all

Upvotes: 0

Related Questions