0

I have used below json string to deserialize as DataTable

string json = "[{\"clientID\":\"1788\",\"projectID\":\"19\"}]";
var data = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

But i got the below exception

enter image description here

if i have tried to deserialize using List which has deserialzed successfully.

var dat = JsonConvert.DeserializeObject<List<Client>>(json);

But i want to Deserialize using DataTable.

Please suggest me if i have missed anything

Thanks in Advance

klashar
  • 2,519
  • 2
  • 28
  • 38
MOHANRAJ G
  • 75
  • 9
  • Please do not post screenshots of the exception, but text, including the stacktrace. You can see when clicking at "View Detail..." – Sentry Feb 15 '17 at 14:58
  • Was this object serialized using as a source a DataTable object? Otherwise it won't have the proper serialization format to convert it to a DataTable – Carles Feb 15 '17 at 14:59
  • you cannot directly converter JSON to system.DataTable format, you need to set class for the same. – Abhinav Feb 15 '17 at 15:00
  • Try removing the outer square brackets. Or adding extra figure brackets around. – user5226582 Feb 15 '17 at 15:00
  • Thanks for your suggestion, I have tried like this but still i am facing the issue **string json = "{{\"clientID\":\"1788\",\"projectID\":\"19\"}}";** – MOHANRAJ G Feb 15 '17 at 15:40

2 Answers2

1

You need to create class for your JSON object, in my case I can give you example..

public class TagValueConfig
{
    public string DisplayName { get; set; }
    public int MinTagValue { get; set; }
    public int MaxTagValue { get; set; }
}

Then you can deserialize it to object array

 TagValueConfig[] ObjTagConfigData = JsonConvert.DeserializeObject<TagValueConfig[]>(ConfigTagString);

once you get Deserialize array, convert it to DataTable format..

or use Extension method.

Hope this helps.. :)

Abhinav
  • 1,202
  • 1
  • 8
  • 12
0

You cannot deserialize a List<T> to a DataTable. Instead you can first get a List<T> and then convert it to DataTable.

This: Converting a List to a DataTable could be a good staring point.

The code from the article:

public static class ExtensionMethods
        {
        /// <summary>
        /// Converts a List to a datatable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(this IList<T> data)
            {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            DataTable dt = new DataTable();
            for (int i = 0; i < properties.Count; i++)
                {
                PropertyDescriptor property = properties[i];
                dt.Columns.Add(property.Name, property.PropertyType);
                }
            object[] values = new object[properties.Count];
            foreach (T item in data)
                {
                for (int i = 0; i < values.Length; i++)
                    {
                    values[i] = properties[i].GetValue(item);
                    }
                dt.Rows.Add(values);
                }
            return dt;
            }
        }

With the generic code from the article you can do something like:

var dat = JsonConvert.DeserializeObject<List<Client>>(json);

and then to convert:

var dataTable = dat.ToDataTable();
Habib
  • 219,104
  • 29
  • 407
  • 436