converting object to two dimensional array in C#
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开发者_StackOverflow, 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?
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.
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.
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.
精彩评论