C# 将对象转换为二维数组

发布于 2024-11-27 12:38:03 字数 1564 浏览 0 评论 0原文

这应该很简单,但我真的不知道如何找到一种方法来做到这一点......

我正在使用.NET 4.0。我有一个用十进制数字填充的 object[12],并想用它来填充 Double 的 Excel 范围 [1,12]。我正在使用 JavaScriptSerializer 转换 JSON:

JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
var jsonData = javascriptSerializer.Deserialize<dynamic>("{data:[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22]}");

它应该是一行,但我能想到的唯一有效的方法是:

var myArray = new Double[1, 12];
// the cast to Double is necessary as json data is of type decimal, however excel seems to only accepts Double here
for (int i = 0; i < 12; i++) { PrimeShr[0,i] = (Double) jsonData["data"][i]; };
// sheet is of type Microsoft.Office.Interop.Excel.Worksheet
sheet.Range["myExcelRangeName"].Value2 = myArray;

或者仍然相当可怜,但至少在一行中:

sheet.Range["Input_PremiumSHR"].Value2 = new Double[1, 12] { { (Double)jsonData["data"][0], (Double)jsonData["data"][1], (Double)jsonData["data"][2], (Double)jsonData["data"][3], (Double)jsonData["data"][4], (Double)jsonData["data"][5], (Double)jsonData["data"][6], (Double)jsonData["data"][7], (Double)jsonData["data"][8], (Double)jsonData["data"][9], (Double)jsonData["data"][10], (Double)jsonData["data"][11] } };

我期望类似的东西可以工作,但从我收集的情况来看,初始化器不会这样工作:

sheet.Range["Input_PremiumSHR"].Value2 = new Double[1, 12] { new System.Collections.ArrayList( jsonData["data"] ).ToArray() };

我有很多类似的情况(其中一个带有 [12,1] ,这可能会更困难),我不想为每个情况而挣扎就这么多。有没有办法写得简单一点?

This one should be simple, but I don't really know how to find a way to do it...

I am using .NET 4.0. I have an object[12] filled with decimal numbers, and want to use it to fill an Excel range of Doubles [1,12]. I am converting JSON using JavaScriptSerializer:

JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
var jsonData = javascriptSerializer.Deserialize<dynamic>("{data:[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22]}");

It should be a one-liner, but the only working thing I can come up with is:

var myArray = new Double[1, 12];
// the cast to Double is necessary as json data is of type decimal, however excel seems to only accepts Double here
for (int i = 0; i < 12; i++) { PrimeShr[0,i] = (Double) jsonData["data"][i]; };
// sheet is of type Microsoft.Office.Interop.Excel.Worksheet
sheet.Range["myExcelRangeName"].Value2 = myArray;

or still rather pitiful, but at least in one line:

sheet.Range["Input_PremiumSHR"].Value2 = new Double[1, 12] { { (Double)jsonData["data"][0], (Double)jsonData["data"][1], (Double)jsonData["data"][2], (Double)jsonData["data"][3], (Double)jsonData["data"][4], (Double)jsonData["data"][5], (Double)jsonData["data"][6], (Double)jsonData["data"][7], (Double)jsonData["data"][8], (Double)jsonData["data"][9], (Double)jsonData["data"][10], (Double)jsonData["data"][11] } };

I'd expect something like that to work, but from what I gather initializers won't work this way :

sheet.Range["Input_PremiumSHR"].Value2 = new Double[1, 12] { new System.Collections.ArrayList( jsonData["data"] ).ToArray() };

I have quite a few similar cases (one with [12,1] which probably could be harder) and I don't want to struggle with each one that much. Is there a way to write it a bit more simpler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ら栖息 2024-12-04 12:38:03

为什么一定要使用动态呢?为什么不创建一个表示 jsonData 的简单类。

 public class SomeData
 {
      public double[] data { get; set; }
 }

然后反序列化后,

 JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
 var jsonData = javascriptSerializer.Deserialize<SomeData>("{data:[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22]}");

就可以直接访问jsonData.data了。

Why do you have to use dynamic? Why don't you make a simple class representing jsonData.

 public class SomeData
 {
      public double[] data { get; set; }
 }

Then after deserialization,

 JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
 var jsonData = javascriptSerializer.Deserialize<SomeData>("{data:[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22]}");

you would be able to access jsonData.data directly.

坏尐絯 2024-12-04 12:38:03

您可以创建一个扩展方法,将一维数组(或任何其他集合)转换为具有一列的二维数组:

static T[,] ToColumn<T>(this IEnumerable<T> sequence)
{
    var items = sequence.ToArray();

    var column = new T[1, items.Length];

    for (int i = 0; i < items.Length; i++)
        column[0, i] = items[i];

    return column;
}

使用该方法,您可以编写如下代码:

var data = (object[])jsonData["data"];

var column = data.Cast<decimal>().Select(x => (double)x).ToColumn();

sheet.Range["myExcelRangeName"].Value2 = column;

可以将其写在一个列上行,但我认为这会损害可读性。

You can create an extension method that turns a single-dimensional array (or any other collection) into a 2D array with one column:

static T[,] ToColumn<T>(this IEnumerable<T> sequence)
{
    var items = sequence.ToArray();

    var column = new T[1, items.Length];

    for (int i = 0; i < items.Length; i++)
        column[0, i] = items[i];

    return column;
}

Using that you can write the code like this:

var data = (object[])jsonData["data"];

var column = data.Cast<decimal>().Select(x => (double)x).ToColumn();

sheet.Range["myExcelRangeName"].Value2 = column;

You could write that on one line, but I think that would hurt readability.

吝吻 2024-12-04 12:38:03
var values = ((object[]) jsonData["data"]).ConvertAll<double>();
sheet.Range["Input_PremiumSHR"].Value2 = new double[1, values.Length] { values };

但我同意Taesung的观点,显式数据结构会更好

var values = ((object[]) jsonData["data"]).ConvertAll<double>();
sheet.Range["Input_PremiumSHR"].Value2 = new double[1, values.Length] { values };

But I agree with Taesung that explicit data structure would be better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文