用这种格式保存数据的c对象是什么

本文关键字:对象 是什么 数据 保存 格式 | 更新日期: 2023-09-27 17:58:04

我有这个字符串

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个阵列并将它们填充到正确的数据中。效果很好。然而,我的altimate目标是将最终结果传输到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};

现在,我所需要的只是将每个数组的标签和数组保存在一行中。

用这种格式保存数据的c对象是什么

我建议您使用字典。具体而言,

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} },    
}

希望这就是您所期望的

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