JSON日期不正确

时间:2012-11-29 19:00:41

标签: c# servicestack

我在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小时。

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

1 个答案:

答案 0 :(得分:1)

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

  • 您的网络服务以UTC格式返回日期/时间。
  • 您的代码正在将这些日期/时间解析为UTC日期和时间。