我无法使用以下(测试)代码。这使我在此行出现“参数计数不匹配”错误
dataTable.Merge(CreateDataTableFromObject(info.GetValue(inputObject)));
整个代码如下:
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;
}
请注意,我正在尝试对JsonConvert对象和JObject做相同的事情-执行
时出现错误CreateDataTableFromObject(object inputObject)
在JObject对象上,而不在JsonConvert对象上。
我需要一个JObject解决方案,因为我必须处理一些未知的json字符串,我需要将其放入DataTable中(列名是属性名,行值是json对象的值)。我已经省略了用法。 我看不到任何其他stackoverflow文章都回答了这个问题。
答案 0 :(得分:0)
好的-我发现我有点纠结了。并得出以下解决方案:
module.exports{install}
现在只有一个问题,是否可以重新编写,以便
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;
}
可以是
ToDataTable(List<JObject> list)
相反-我还没有找到答案...