将对象数组传递到请求主体

时间:2018-08-27 21:06:56

标签: c# rest api

我有一个定义如下的对象。

public class ILT
{
    public items items;
    public options options;
}

public class items
{
    public string course_code { get; set; }
    public string session_code { get; set; }
    public string date_name { get; set; }
    public string date { get; set; }
    public string time_start { get; set; }
    public string time_end { get; set; }
    public string location_name { get; set; }
    public string location_address { get; set; }
    public string location_country { get; set; }

    public items() { }

    public items(string course_code, string session_code, string date_name, 
    string date, string time_start, string time_end, string location_name, 
    string location_address, string location_country)
        {
            this.course_code = course_code;
            this.session_code = session_code;
            this.date_name = date_name;
            this.date = date;
            this.time_start = time_start;
            this.time_end = time_end;
            this.location_name = location_name;
            this.location_address = location_address;
            this.location_country = location_country;
        }
    }

我正在尝试将该对象传递给RestfulAPI请求主体。 “ items”属性应该是一个对象数组。

JSon的格式应如下:

{
  "items": [
    {
      "course_id": 6,
      "session_code": "my session code",
      "session_name": "my session name",
      "session_maximum_enrollments": 20,
      "session_last_subscription_date": "2018-10-27",
      "completion_type": "Evaluation",
      "score_base": 100,
      "date_name": "my date name",
      "date": "2018-10-28",
      "timezone": "America/New_York",
      "time_start": "08:00:00",
      "time_end": "12:00:00",
      "location_name": "my location name",
      "location_address": "10850 W. Park Place Suite 600, Milwaukee, WI 53225",
      "location_country": "UNITED STATES OF AMERICA"
    }
  ],
  "options": {
    "update_session_info": true
  }
}

我很难将项目放入数组中。我正在尝试将对象初始化为请求主体,如下所示:

public bool CreateILT()
        {
            if (String.IsNullOrEmpty(Token))
                Token = request.GetToken();

            ILT classroom = new ILT
            {
                items = new items[0]
                (   
                    course_code = "APS_CLASSROOM",
                    session_code = "APS_CLASSROOM",
                    date_name = "August 27, 2018",
                    date = "2018-10-27",
                    time_start = "08:00:00",
                    time_end = "17:00:00",
                    location_name = "Crisis Prevention Institute",
                    location_address = "10850 W. Park Place Suite 600, Milwaukee, WI 53225",
                    location_country = "UNITED STATES OF AMERICA"
                ),
                options = new options
                {
                    update_session_info = true
                }

            };

            dynamic response = request.Request_POST("/learn/v1/ilt/session/batch", Token, classroom);
            if (response.data.success.ToString() == "True")
                success = true;

            return success;
        }

我可以像这样初始化一个对象数组吗?调整时出现各种错误。上面的代码错误地表明了对象的每个成员在当前上下文中不存在该对象。

1 个答案:

答案 0 :(得分:1)

您的班级变量脱节是错误的。它存储对象,而不是对象的数组/列表。而且我看不到您的public E get(int index) { Objects.checkIndex(index, size); return elementData(index); } E elementData(int index) { return (E) elementData[index]; } 类。你有权利吗?

应声明如下:

options

您应该按如下所示对其进行初始化:

public class ILT
{
    public List<items> items;
    public options options;
}