如果希望从一个的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"
},...
从切口正患 - 粘贴 - 炎? :)你已经迭代了整个枚举,但是用if语句过滤掉了与当前不一样的偏移量。 – Spoike 2009-01-14 07:02:28
我忽略了如果在代码:( – gyurisc 2009-01-14 07:28:56