[英]JSON.NET unable to serialize excel array
I am getting an IndexOutOfRangeException when trying to use JSON.NET to serialize and object array retrieved from an excel range. 尝试使用JSON.NET序列化和从Excel范围检索的对象数组时,出现了IndexOutOfRangeException异常。 I think the reason that this is happening is that the excel array is not zero based.
我认为发生这种情况的原因是excel数组不是基于零的。
If i create an array like this in .NET, the dimensions would be {object[4,2]}
如果我在.NET中创建这样的数组,则尺寸将为
{object[4,2]}
object[,] data = {
{ 1111, 1111 },
{ 1112, 1111 },
{ 1113, 1111 },
{ 1114, 1111 },
};
When i look at an array retrieved from Excel however, i see: 但是,当我查看从Excel检索的数组时,我看到:
{object[1..4, 1..2]}
Is this a bug in JSON.Net. 这是JSON.Net中的错误吗? I think if i can make the array zero-based, it would work.
我认为,如果我可以使数组从零开始,那么它将起作用。 but not sure how to do that easily without a maybe creating a new array and copying values one by one.
但不确定如何轻松地做到这一点,而无需创建新数组并一一复制值。 Any thoughts?
有什么想法吗?
UPDATE: I tried the following, and looks like this code will do the trick to copy the array starting at index 1 to an array that is zero-based. 更新:我尝试了以下操作,并且看起来这段代码可以完成将索引1开始的数组复制到基于零的数组的技巧。 Still, i never knew .net arrays can be non-zero based.
不过,我从来不知道.net数组可以基于非零。
object[,] zeroBasedArr = new object[oneBasedArr.GetLength(0), oneBasedArr.GetLength(1)];
Array.Copy(oneBasedArr, 1, darr, 0, oneBasedArr.GetLength(0) * oneBasedArr.GetLength(1));
Reposting answer in case it helps someone.. Basically i had to make the array zero-based for JSON.Net to be able to properly searilize it: 重新发布答案以防万一。.基本上我必须使JSON.Net的数组从零开始,以便能够对其进行适当的Searilize:
object[,] zeroBasedArr = new object[oneBasedArr.GetLength(0), oneBasedArr.GetLength(1)];
Array.Copy(oneBasedArr, 1, darr, 0, oneBasedArr.GetLength(0) * oneBasedArr.GetLength(1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.