我正在使用Azure函数来创建一个excel文件,但是我有点难以迭代输入。
这是输入:
{
"data": {
"Header1": {
"body": [
"1",
"2",
"3"
]
}
}
}
我想先获取“数据”中对象的键,然后将其放在最前面的单元格中,但是我要迭代并从中获取键有点困难。那就是我想要做的:
Data data = JsonConvert.DeserializeObject<Data>(body as string);
foreach (var item in data.data.Keys)
{
// For each iteration, go to the right. In this case, `i` doesn't exist.
// Is there a way to do it then?
Cells[0, i] = item; // Not item, the key of item.
}
Header1只是一个示例,键可以是任何单词。
数据类:
public class Data
{
public dynamic data { get; set; }
}
为了获得更好的结构,您必须放弃使用动态变量并编写一些类,这是一个示例:
public class DataContainer
{
public Dictionary<string, Header> Data { get; set; }
}
public class Header
{
public int[] Body { get; set; }
}
您现在所要做的就是像这样使用它:
string json = "{\"data\":{\"Header1\":{\"body\":[\"1\",\"2\",\"3\"]}}}";
DataContainer data = JsonConvert.DeserializeObject<DataContainer>(json);
这将为您提供具有正确信息的DataContainer实例,您可以像这样迭代它:
foreach (var kvp in data.Data)
{
// kvp.Key holds the keys (Header1, etc...)
// kvp.Value holds a Header instance
// kvp.Value.body holds the integer array you have
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句