JSON Serializacja

0

Witam,

Otóż chciałbym utworzyć Jsona takiego jak poniżej ale nie wiem jak utworzyć w nim tablicę:


{
  "Subject": "API API API",
  "Body": {
    "ContentType": "HTML",
    "Content": "I think it will meet our requirements!"
  },
  "Start": {
      "DateTime": "2016-05-17T18:00:00",
      "TimeZone": "Pacific Standard Time"
  },
  "End": {
      "DateTime": "2016-05-17T19:00:00",
      "TimeZone": "Pacific Standard Time"
  },
  "Attendees": [
    {
      "EmailAddress": {
        "Address": "[email protected]",
        "Name": "Janet Schorr"
      },
      "Type": "Required"
    }
  ]
} 

Utworzyłem model :

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace DNSAP.API.Models
{
    public class _Event
    {
        public _EmailAddress EmailAddressl;
       
        public string Subject;
        public _Body Body;
        public _Date Start;
        public _Date End;
        public _EmailAddress EmailAddress;
        
    }
    public class _Body
    {

        public string ContentType = "HTML";
        public string Content;
       
    }
    public class _Date
    {
        public string DateTime;
        public string TimeZone = "Pacific Standard Time";
    }

    public class _EmailAddress
    {
        public string Address;
        public string Name;
    }
    public class _Type
    {
        public string Type = "Required";
    }
}

Oraz kontrolkę :

public object AddEvent()
        {
            _Event Event = new _Event()
            {

                Subject = "",
                Body = new _Body() { Content = "HEJO HEJO" },
                Start = new _Date() { DateTime = "2016-05-17T18:00:00" },
                End = new _Date() { DateTime = "2016-05-17T18:30:00" },
                EmailAddressl = new _EmailAddress() { Address = "", Name = "" },
                 
            };
           return 0;

        } 

Tutaj mam problem z utworzeniem

"Attendees": [
    {
      "EmailAddress": {
        "Address": "[email protected]",
        "Name": "Janet Schorr"
      },
      "Type": "Required"
    }
  ]
} 

Nie za bardzo wiem jak mam upchać dwa obiekty do list/tablicy.

Czy ktoś może robił coś podobnego ? Lub wie jak to zrobić.

Z góry dziękuje za pomoc.

1

Ja bym to zrobił dokładnie tak, jak jest zrobione w tym JSON-ie, tam jako pole Attendees masz listę obiektów pewnego typu.

public class Attendee
{
    public _EmailAddress EmailAddress { get; set; }
    public _Type Type { get; set; }
}

public class _Event
{
    public string Subject;
    public _Body Body;
    public _Date Start;
    public _Date End;
    public List<Attendee> Attendees;
}

Aczkolwiek jeszcze dwie rzeczy się rzucają - _Date.DateTime to raczej nie jest string, ale System.DateTime i go można odpowiednio zmieniać na stringa (zgodnie z tym zapisem - to jest ISO ileśtam). Natomiast _Type to raczej nie jest klasa posiadająca określoną właściwość Type, ale dla mnie - enumeracja, która zawiera oprócz "Required" inne możliwości (jakie są?), coś w stylu:

public enum _Type
{
    Required, 
    Attending
}

Tylko, że jej wartość zapisuje się do JSON-a jako string, nie jako odpowiadającą wartość liczbową z enuma.

0

Dzięki, o to właśnie chodziło :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1