【求助】 C#应用JSON转换DataTable 范围空

newnazi 2021-04-18 12:38:18
运行到  这行   ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(strJson); 结果 arrayList.Count =0 了  请问应该如何修改,输入Json为 JsStr = "{ \"code\":\"1001\",\"Id\":\"81\",\"Name\":\"测试公司\"}"; //Json 字符串


函数代码如下


public static DataTable JsonToDataTable4(string strJson)
{
DataTable dataTable = new DataTable();  //实例化
            DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
                ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(strJson);

if (arrayList.Count > 0)
{
                    //表头
                    int maxColCount = GetArrayListMaxKeysCount(arrayList);//获取最多字段数的行
                    foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == 0)
{
result = dataTable;
return result;
}
                        //获取最大的Keys 字段集合作为表头,避免行字段字段不一致
                        if (dataTable.Columns.Count == 0 && dictionary.Keys.Count<string>() == maxColCount)
{
foreach (string current in dictionary.Keys)
{
if (dictionary[current] != null)
{
if (dictionary[current].GetType() == typeof(int))//避免数字丢失,若是int数据转成decimal
                                    {
dataTable.Columns.Add(current, typeof(decimal));
}
else
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
else//当数据为null时,默认为字符串格式
                                {
dataTable.Columns.Add(current, typeof(string));
}
}
break;
}
}

                    //为表赋值赋值
                    foreach (Dictionary<string, object> dictionary in arrayList)
{
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
if (dictionary[current] != null)
dataRow[current] = dictionary[current];
}

dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
                        dataTable.AcceptChanges();
}
}
}
catch
{
throw;
}
result = dataTable;
return result;
}

public static int GetArrayListMaxKeysCount(ArrayList sampleList)
{
try
{
int MaxKeysCout = 0;
foreach (Dictionary<string, object> dictionary in sampleList)
{
int temp = dictionary.Keys.Count<string>();
if (temp > MaxKeysCout)
{
MaxKeysCout = temp;
}
}
return MaxKeysCout;
}
catch (Exception ex)
{
throw ex;
}

}
...全文
280 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
极客诗人 2021-04-19
  • 打赏
  • 举报
回复
对象。。。。 集合。。
by_封爱 版主 2021-04-19
  • 打赏
  • 举报
回复
不知道你累不累...反正我很累.. sp说的没错.. 你是对象 但是你要转集合.. 这本身就有问题.. 所以你要强制加[] 来使对象变成集合. 然后在说你写这么多代码... 你都知道反序列化这个东西.. 那干嘛不直接反序列化成datatable呢? 不就一句话就搞定了吗
longsky 2021-04-19
  • 打赏
  • 举报
回复
JsStr = "[{ \"code\":\"1001\",\"Id\":\"81\",\"Name\":\"测试公司\"},{ \"code\":\"1002\",\"Id\":\"82\",\"Name\":\"测试2公司\"},{ \"code\":\"1003\",\"Id\":\"83\",\"Name\":\"测试3公司\"}]"; 如果json是这样的,你那样写就是对的。
正怒月神 2021-04-19
  • 打赏
  • 举报
回复
你反序列化的是 arraylist,但是塞的不是数组啊,是一个单个对象。。。
newnazi 2021-04-18
  • 打赏
  • 举报
回复
引用 1 楼 以专业开发人员为伍 的回复:
.net 对象是一个列表,而你的 json 描述的是一个对象实例(不是实例的列表),这本身就概念不对。
您能说的更加直白简单一些好吧
  • 打赏
  • 举报
回复
.net 对象是一个列表,而你的 json 描述的是一个对象实例(不是实例的列表),这本身就概念不对。

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧