[英]jObject.Parse and JsonConvert.DeserializeObject data into a DataTable resulting in Parameter Count Mismatch exception
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请注意,我正在尝试对 JsonConvert 对象和 JObject 执行相同的操作 - 执行时出现错误
CreateDataTableFromObject(object inputObject)
on the JObject object and not on the JsonConvert object.在 JObject 对象上而不是在 JsonConvert 对象上。
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).我需要 JObject 的解决方案,因为我必须处理一些未知的 json 字符串,我需要将其放入 DataTable(列名称是属性名称,行值是 json 对象的值)。 I have omitted the usings.
我省略了使用。 I don't see that this is answered by any of the other stackoverflow articles.
我没有看到任何其他 stackoverflow 文章都回答了这个问题。
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...相反 - 我还没有找到答案......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.