-1

I have trouble with the following (test) code. This gives me a "Parameter Count Mismatch" error at the line

dataTable.Merge(CreateDataTableFromObject(info.GetValue(inputObject)));

The entire code looks like this:

public object SerializeThis(DataTable dataTable1, DataTable dataTable2)
        {
        string jsonString = @"{'EquipmentNumber':'CP5301078','Data_General_Exp': {'Authgrp':'CP01','Objecttype':'9A1B'}}";

            var jConvertObejct = (JsonConvertObject)JsonConvert.DeserializeObject(jsonString, typeof(JsonConvertObject));
            var jObject = JObject.Parse(jsonString);

            dataTable1 = CreateDataTableFromObject(jConvertObejct);
            dataTable2 = CreateDataTableFromObject(jObject);

            return jConvertObejct;
        }

public DataTable CreateDataTableFromObject(object inputObject)
        {
            DataTable dataTable = new DataTable();
                Type type = inputObject.GetType();
                var properties = type.GetProperties();
                PropertyInfo info;

                for (int i = 0; i < properties.Length; i++)
                {
                    info = properties[i];
                    if (info.GetValue(inputObject).GetType().GetProperties().Count() > 2)
                        dataTable.Merge(CreateDataTableFromObject(info.GetValue(inputObject)));
                    else
                        if (!dataTable.Columns.Contains(info.Name))
                        dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
                }
            return dataTable;
        }

Note that I am trying to do the same thing with both the JsonConvert object and the JObject - the error is emerging when executing the

CreateDataTableFromObject(object inputObject) 

on the JObject object and not on the JsonConvert object.

I need a solution for the JObject as I have to handle some unknown json strings, which I need to put in to a DataTable (column names being the property names and row values being the values of the json objects). I have omitted the usings. I don't see that this is answered by any of the other stackoverflow articles.

  • Why are you working with DataTables in 2019? – silkfire Jun 24 '19 at 17:37
  • 2
    You're using reflection to get the **c# properties** of the type `JObject`: https://www.newtonsoft.com/json/help/html/Properties_T_Newtonsoft_Json_Linq_JObject.htm. Those have nothing to do with the **JSON contents** of the `jObject` instance. – dbc Jun 24 '19 at 18:01
  • @silkfire: why not use DataTables? Anyway its part of the architecture of the application, which I am adding functionality to that dictates using DataTables... – Torben Hjalholt Jun 26 '19 at 07:29

1 Answers1

0

OK - I found that I had tangled things a bit up. And came to this solution:

        public static DataTable DeSerializeThis(string jsString)
        {
            const string json1 = @"{""EquipmentNumber"":""CP1"",""Authgrp"":""CP01"",""Objecttype"":""9A1A""}";
            const string json2 = @"{""EquipmentNumber"":""CP2"",""Authgrp"":""CP02"",""Objecttype"":""9B1B""}";
            List<JObject> list = new List<JObject>();
            list.Add(JObject.Parse(json1));
            list.Add(JObject.Parse(json2));
            DataTable table = ToDataTable(list);
            return table;
        }
        static public DataTable ToDataTable(List<JObject> list)
        {
            DataTable dataTable = new DataTable();
            int i = 0;
            foreach (JToken content in list.ToList<JToken>())
            {
                dataTable.Rows.Add();
                foreach (JProperty prop in content)
                {
                    if (i == 0)
                    {
                        dataTable.Columns.Add(prop.Name);
                    }
                    dataTable.Rows[i][prop.Name] = prop.Value;
                }
                i++;
            }
            return dataTable;
        }

Only now is the question if this could be re-written so that the

ToDataTable(List<JObject> list)

Could be of a

List<T> 

instead - I haven't found the answer for that...

  • You could always just convert each object to a `JObject` using `JObject.FromObject()`. – dbc Jun 26 '19 at 21:27