1

我在 JSON 中表示日期时遇到问题。我使用 Service Stack 作为 Web 服务来获取数据。我在服务器端的代码如下:

    public object Execute(GetNoPatientList request)
    {
        NoPatientList _noPatientList = new NoPatientList();

        List<string> _noMatchPatientList = new List<string>();
        List<NoPatientList> _newList = new List<NoPatientList>();
        try
        {
            using (SqlConnection cn = new SqlConnection(Database.WaldenWebConnection))
            {
                cn.Open();
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandText = "select [DateTimeStamp] as DateCreated,[ID],[PatientMRN],[FirstName],[MiddleName]"
                        + " ,[LastName],convert(varchar,[DOB],101) as DOB,[Sex],[Note],[Source] as Interface"
                        + " from PatientNoMatch"
                        + " where FoundMatch = 'F'"
                        + " and Show = 'T'"
                        + " order by DateTimeStamp desc";

                    SqlDataReader dr = cm.ExecuteReader();
                    while (dr.Read())
                    {
                        NoPatientList _noPatientList1 = new NoPatientList();

                        _noPatientList1.PatientMRN = dr["PatientMRN"].ToString();
                        _noPatientList1.FirstName = dr["FirstName"].ToString();
                        _noPatientList1.MiddleName = dr["MiddleName"].ToString();
                        _noPatientList1.LastName = dr["LastName"].ToString();
                        _noPatientList1.DOB = dr["DOB"].ToString();
                        _noPatientList1.Sex = dr["Sex"].ToString();
                        _noPatientList1.Note = dr["Note"].ToString();
                        _noPatientList1.DateCreated = dr.GetDateTime(0);
                        _noPatientList1.Interface = dr["Interface"].ToString();

                        _newList.Add(_noPatientList1); 
                    }
                    return _newList;
                }
            }
        }
        catch
        {
            return _newList;
        }
    }

类型表示如下:

[DataContract]
public class NoPatientList
{
    [DataMember]
    public string ID { get; set; }

    [DataMember]
    public string PatientMRN { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string MiddleName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public string Sex { get; set; }

    [DataMember]
    public string DOB { get; set; }

    [DataMember]
    public string Note { get; set; }

    [DataMember]
    public DateTime DateCreated { get; set; }

    [DataMember]
    public string Interface { get; set; }

}

Silverlight 应用程序正在通过以下调用使用 Web 服务:

/InterfaceUtility/servicestack/json/syncreply/

Silverlight 应用程序正在使用以下代码将代码处理成网格

    private void GetNoPatientMatchData()
    {
        try
        {
            gridViewNoMatch.ItemsSource = null; 
        }
        catch { }
        _client = new WebClient();
        _client.OpenReadCompleted += (a, f) =>
        {
            if (!f.Cancelled && f.Error == null)
            {
                _listOfNoPatientsMatches = new List<NoPatientList>();                    
                MemoryStream _memoryStream = new MemoryStream();
                f.Result.CopyTo(_memoryStream);
                _memoryStream.Position = 0;

                StreamReader _streamReader = new StreamReader(_memoryStream);

                string _memoryStreamToText = _streamReader.ReadToEnd();

                List<NoPatientList> _deserializedNoPatientList = (List<NoPatientList>)Newtonsoft.Json.JsonConvert.DeserializeObject(_memoryStreamToText, typeof(List<NoPatientList>));                   
                gridViewNoMatch.ItemsSource = _deserializedNoPatientList;
            }
            else
            {
                MessageBox.Show(f.Error.Message,
                    "Error", MessageBoxButton.OK);
            }
        };

        _client.OpenReadAsync(new Uri(_serviceUri + "getnopatientlist"));

问题是 DateTime 字段上的时间似乎总是 6 小时。

关于发生了什么的任何想法?

4

1 回答 1

1

这可能是一个时区问题。检查:

  • 您的网络服务以 UTC 格式返回您的日期/时间。
  • 您的代码将这些日期/时间解析为 UTC 日期和时间。
于 2012-11-29T19:03:02.027 回答