简体   繁体   中英

Newtonsoft deserialize a same date return different output

I'm deserializing a JSON using Newtonsoft for my Xamarin application, I am facing some issue when deserializing DateTime.

I have tried using the same code on asp.net C#, and storing the data into a dataset. The C# gave me the right output. However when it goes to Xamarin form, it produces the wrong output.

Expected result should be 2/4/2019 12:00:00 AM .

I have no idea how the 4/1/2019 4:00:00PM came from.

Wrong output using Xamarin

错误的输出

Right output using C#

在此处输入图片说明

using Xamarin

public List<GetFleet> GetDefaults(string xUserID)
    {
        string inJson = 
        List<GetFleet> tempList = new List<GetFleet>();
        try
        {
            Uri serverUri2 = new Uri(inJson.ToString());

            string rs2 = RequestGeoHttpAsString(serverUri2);

            DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(rs2);

            DataTable dataTable = dataSet.Tables["Table1"];

            foreach (DataRow row in dataTable.Rows)
            {
                tempList.Add(new GetFleet
                {
                    FleetID = row["registrationNumber"].ToString(),
                    FleetName = row["Location"].ToString(),
                    FleetIgnition = row["Ignition"].ToString(),
                    FleetFuel1 = row["sFuel1"].ToString(),
                    FleetStartTime = row["startTime"].ToString()

                });
            }

        }
        catch (Exception ex)
        {
            string exe = ex.Message;
        }
        return tempList;
    }

using C#

protected void Page_Load(object sender, EventArgs e)
    {
        string url = 
        Uri serverUri2 = new Uri(url.ToString());

        string rs2 = RequestGeoHttpAsString(serverUri2);

        DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(rs2);

        DataTable dataTable = dataSet.Tables["Table1"];

        string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
        Response.Write(json);
    }

    public string RequestGeoHttpAsString(Uri address)
    {
        string result = "";


        // Create the web request  
        HttpWebRequest request = System.Net.WebRequest.Create(address) as HttpWebRequest;

        // Get response  
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            // Get the response stream  
            StreamReader reader = new StreamReader(response.GetResponseStream());

            // Read the whole contents and return as a string  
            result = reader.ReadToEnd();

        }

        return result;
    }

I used another alternative solution on this matter. I converted the date into string/VARCHAR in backend. When it returns in JSON, it would be in text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM