在此格式中保存数据的c#对象是什么

what is the c# object to save the data in this formate
2021-03-07
  •  译文(汉语)
  •  原文(英语)

我有这串

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对象的对象是什么?

我正在使用json.net库,所以我可以轻松地将任何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};

现在,我需要做的就是将每个数组的标签和该数组保存在一行中.

解决过程1

我建议您使用字典.特别,

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:
我复制了您的代码,但是出现很多红色错误.例如,no overload for method take 1 argumentinvalid anoymous type member declared......为什么吗?
速聊2:
请给我几分钟以修复它.感谢您的耐心等待.
速聊3:
你能在这里关注我吗stackoverflow.com/questions/23028526/…
解决过程2

希望这就是你所期望的

    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;

What I need

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]

what I have tried

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 am using json.net library so I can easily change any c# object to json object

Edit

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.

Solutions1

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} },    
}
Talk1:
I copied your code, but I got a lot of red errors. for example, no overload for method take 1 argument and invalid anoymous type member declared ... why please?
Talk2:
please give me a few minutes to fix it. Thanks for being patient.
Talk3:
could you follow me here please stackoverflow.com/questions/23028526/…
Solutions2

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" ] ]

转载于:https://stackoverflow.com/questions/23027983/what-is-the-c-sharp-object-to-save-the-data-in-this-formate

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入