如果想要来自 WebAPI 调用的 json 输出:
using System;
using System.Collections.Generic;
namespace MyProject.ViewModels
{
public class TimeZoneViewModel
{
public readonly List<CTimeZone> CTimeZones;
public TimeZoneViewModel()
{
CTimeZones = new List<CTimeZone>();
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
{
var tz = new CTimeZone(z.Id, z.DisplayName, z.BaseUtcOffset);
CTimeZones.Add(tz);
}
}
}
public class CTimeZone
{
public string Id { get; set; }
public string DisplayName { get; set; }
public TimeSpan BaseUtcOffset { get; set; }
public CTimeZone(string id, string displayName, TimeSpan utcOffset)
{
Id = id;
DisplayName = displayName;
BaseUtcOffset = utcOffset;
}
}
}
然后在 WebAPI 中使用它:
[HttpGet("Api/TimeZones")]
public JsonResult GetTimeZones()
{
return Json(new TimeZoneViewModel().CTimeZones);
}
输出:
[{
"id": "Dateline Standard Time",
"displayName": "(UTC-12:00) International Date Line West",
"baseUtcOffset": "-12:00:00"
},
{
"id": "UTC-11",
"displayName": "(UTC-11:00) Coordinated Universal Time-11",
"baseUtcOffset": "-11:00:00"
},
{
"id": "Aleutian Standard Time",
"displayName": "(UTC-10:00) Aleutian Islands",
"baseUtcOffset": "-10:00:00"
},
{
"id": "Hawaiian Standard Time",
"displayName": "(UTC-10:00) Hawaii",
"baseUtcOffset": "-10:00:00"
},...