简体   繁体   中英

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. I think the reason that this is happening is that the excel array is not zero based.

If i create an array like this in .NET, the dimensions would be {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:

 {object[1..4, 1..2]}

Is this a bug in 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. Still, i never knew .net arrays can be non-zero based.

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:

object[,] zeroBasedArr = new object[oneBasedArr.GetLength(0), oneBasedArr.GetLength(1)];
Array.Copy(oneBasedArr, 1, darr, 0, oneBasedArr.GetLength(0) * oneBasedArr.GetLength(1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM