C# 将对象转换为二维数组
这应该很简单,但我真的不知道如何找到一种方法来做到这一点......
我正在使用.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 Double
s [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么一定要使用动态呢?为什么不创建一个表示 jsonData 的简单类。
然后反序列化后,
就可以直接访问jsonData.data了。
Why do you have to use dynamic? Why don't you make a simple class representing jsonData.
Then after deserialization,
you would be able to access jsonData.data directly.
您可以创建一个扩展方法,将一维数组(或任何其他集合)转换为具有一列的二维数组:
使用该方法,您可以编写如下代码:
您可以将其写在一个列上行,但我认为这会损害可读性。
You can create an extension method that turns a single-dimensional array (or any other collection) into a 2D array with one column:
Using that you can write the code like this:
You could write that on one line, but I think that would hurt readability.
但我同意Taesung的观点,显式数据结构会更好。
But I agree with Taesung that explicit data structure would be better.