我有这串
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};
现在,我需要做的就是将每个数组的标签和该数组保存在一行中.
我建议您使用字典.特别,
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} },
}
no overload for method take 1 argument
和invalid anoymous type member declared
......为什么吗?希望这就是你所期望的
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" ] ]
I have this string
string[] xInBGraph = { "IVR", "Agents", "Abandoned", "Cancelled" };
and I have these values:
int ivr = 1;
int agents = 2;
int abandoned = 3;
int cancelled = 4;
To make an array for each element in the array xInBGraph
in which the new array should contain one value and the other values are zero. For example This is how the final result will be
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]
making 4 arrays and fill them in the correct data. It works good. However, my altimate goal is to transfer that final result to a json object. I need to return just on json object. but in my case, which is 4 arrays, I must return 4 json objects which is not good for my situation. I need to return just on json object. So, what is the object in c# that can have the mentioned data and can be transfer to one json object?
I made these four arrays:
int[] ivrArray = { Tivr, 0, 0, 0};
int[] agentsArray = { 0, tTotalCallsByAgent, 0, 0 };
int[] abandonedArray = { 0, 0, tTotalAbandoned, 0};
int[] canceledArray = { 0, 0, 0, Tcancel};
Now all I need is something to save the label of each array and the array in one row.
I would suggest you use a Dictionary. Specifically,
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} },
}
no overload for method take 1 argument
and invalid anoymous type member declared
... why please?Hope this is what you are expecting
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);
Output
[ [ "IVR=0", "0", "0", "0" ], [ "0", "Agents=1", "0", "0" ], [ "0", "0", "Abandoned=2", "0" ], [ "0", "0", "0", "Cancelled=3" ] ]
本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文