我有这个字符串
string[] xInBGraph = { "IVR", "Agents", "Abandoned", "Cancelled" };
我有这些价值观:
int ivr = 1;
int agents = 2;
int abandoned = 3;
int cancelled = 4;
为数组xInBGraph
中的每个元素创建一个数组,其中新数组应包含一个值,其他值为零。 例如,这是最终结果
IVR = [ivr =1, 0 , 0 ,0, 0]
Agents = [0, agents=2, 0,0]
Abandoned = [0, 0, abandoned = 3, 0]
Cancelled = [0, 0, 0, cancelled = 0]
制作4个数组并将其填入正确的数据中。它运作良好。但是,我的最终目标是将最终结果传递给json对象。我需要只返回json对象。但在我的情况下,这是4个数组,我必须返回4个json对象,这对我的情况不利。我需要只返回json对象。那么,c#中的对象可以包含所提到的数据并且可以传输到一个json对象吗?
我制作了这四个数组:
int[] ivrArray = { Tivr, 0, 0, 0};
int[] agentsArray = { 0, tTotalCallsByAgent, 0, 0 };
int[] abandonedArray = { 0, 0, tTotalAbandoned, 0};
int[] canceledArray = { 0, 0, 0, Tcancel};
现在我需要的是将每个数组的标签和数组保存在一行中。
答案 0 :(得分:3)
我建议你使用字典。具体地,
Dictionary<string,int[]> dictionary = new Dictionary<string,int[]>()
{
{ "IVR", new int[] {1,0,0,0} },
{ "Agents", new int[] {0,2,0,0} },
{ "Abandoned", new int[] {0,0,3,0} },
{ "Cancelled", new int[] {0,0,0,0} },
}
答案 1 :(得分:0)
希望这是你所期待的
string[] xInBGraph = { "IVR", "Agents", "Abandoned", "Cancelled" };
List<string[]> final = new List<string[]>();
for (int i = 0; i < xInBGraph.Count(); i++)
{
List<string> array = new List<string>();
for (int x = 0; x < xInBGraph.Count(); x++)
{
if (x == i)
{
array.Add(xInBGraph[i].ToString() + "=" + x);
}
else
{
array.Add("0");
}
}
final.Add(array.ToArray());
}
string json = JsonConvert.SerializeObject(final, Formatting.Indented);
的输出强>
[ [ "IVR=0", "0", "0", "0" ], [ "0", "Agents=1", "0", "0" ], [ "0", "0", "Abandoned=2", "0" ], [ "0", "0", "0", "Cancelled=3" ] ]