Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Currently I have a json string in which date format is like this "2012-03-01T08:00:00". whenever try to deserialize, it is showing "does not start with '/Date(' and end with ')/"

What I have tried:

public static T JsonDeserialize<T>(string jsonString)
    {
        //Convert "yyyy-MM-dd HH:mm:ss" String as "\/Date(1319266795390+0800)\/"
        string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";

    MatchEvaluator matchEvaluator = new MatchEvaluator(
            ConvertDateStringToJsonDate);
        Regex reg = new Regex(p);
        jsonString = reg.Replace(jsonString, matchEvaluator);
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
        T obj = (T)ser.ReadObject(ms);
        return obj;
    }
Posted
Updated 8-Nov-17 23:14pm

1 solution

The above code looks overly complicated. Working with JSON does not need to be that hard.

To decode the date, I am assuming, form your information above, that your raw JSON data looks something like:
{ "birthday":"2012-03-01T08:00:00" }

This is a known format for the DataContractJsonSerializer. To decode, you would use something like this class:
C#
[DataContract]
public class Example
{
    [DataMember(Name="birthday")]
    public DateTime Birthday { get; set; }
}

The above class was generated using JsonUtils[^] website with the above JSON data.

To see this in action, I would use this Help Class[^]. It uses Json.Net[^]:
C#
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Support.CSharp
{
    public static class JsonHelper
    {
        public static string FromClass<T>(T data, bool isEmptyToNull = false,
                                          JsonSerializerSettings jsonSettings = null)
        {
            string response = string.Empty;

            if (!EqualityComparer<T>.Default.Equals(data, default(T)))
                response = JsonConvert.SerializeObject(data, jsonSettings);

            return isEmptyToNull ? (response == "{}" ? "null" : response) : response;
        }

        public static T ToClass<T>(string data,
                                   JsonSerializerSettings jsonSettings = null)
        {
            var response = default(T);

            if (!string.IsNullOrEmpty(data))
                response = jsonSettings == null
                    ? JsonConvert.DeserializeObject<T>(data)
                    : JsonConvert.DeserializeObject<T>(data, jsonSettings);

            return response;
        }
    }
}

And to use:
C#
var rawJson = "{ \"birthday\":\"2012-03-01T08:00:00\" }";
var result = JsonHelper.ToClass<Example>(rawJson);

The Working with JSON in C# & VB[^] article goes into a lot more detail (with downloadable fully working code) for working with JSON that you should find helpful. :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900