将列表中的数据转换为JSON格式

时间:2014-01-06 13:21:06

标签: c# json list var

以下是我获取JSON数据的代码,

for (int k = 0; k < 4; k++)
{
  List<HMData> Data_Content = new List<HMData>();
   for (int l = 0; l < 7; l++)
    {  
      Value_LfromList = LValues.ElementAt((k * 7) + l);
      Value_IfromList = IValues.ElementAt((k * 7) + l);
      Value_BfromList = BValues.ElementAt((k * 7) + l);
       Data_Content.Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
     }
     data_list.Add(Data_Content);
  } 
  var chart = new
        {
            type = ChartType
        };
   var data = new { data=data_list };
   var series = new[] { data };
   var obj = new {chart,series};
   string result = jSearializer.Serialize(obj);

我得到的输出如下,

{"chart":{"type":"bubble"},
 "series":
  [{"data":
    [
       [{"x":7,"y":7,"z":49},{"x":7,"y":7,"z":49},{"x":7,"y":9,"z":63},{"x":5,"y":9,"z":45},{"x":4,"y":3,"z":12},{"x":2,"y":6,"z":12},{"x":3,"y":5,"z":15}],
       [{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":9,"y":6,"z":54},{"x":5,"y":7,"z":35},{"x":3,"y":8,"z":24},{"x":4,"y":3,"z":12}],
       [{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":8,"y":7,"z":56},{"x":5,"y":7,"z":35},{"x":3,"y":7,"z":21},{"x":5,"y":8,"z":40}],
       [{"x":3,"y":7,"z":21},{"x":3,"y":7,"z":21},{"x":5,"y":2,"z":10},{"x":5,"y":2,"z":10},{"x":8,"y":6,"z":48},{"x":7,"y":3,"z":21},{"x":6,"y":7,"z":42}]
    ]}
  ]
 }

但我希望输出如下,

{"chart":{"type":"bubble"},
 "series":
  [{"data":[{"x":7,"y":7,"z":49},{"x":7,"y":7,"z":49},{"x":7,"y":9,"z":63},{"x":5,"y":9,"z":45},{"x":4,"y":3,"z":12},{"x":2,"y":6,"z":12},{"x":3,"y":5,"z":15}],

    "data":[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":9,"y":6,"z":54},{"x":5,"y":7,"z":35},{"x":3,"y":8,"z":24},{"x":4,"y":3,"z":12}],

    "data":[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":8,"y":7,"z":56},{"x":5,"y":7,"z":35},{"x":3,"y":7,"z":21},{"x":5,"y":8,"z":40}],

    "data":[{"x":3,"y":7,"z":21},{"x":3,"y":7,"z":21},{"x":5,"y":2,"z":10},{"x":5,"y":2,"z":10},{"x":8,"y":6,"z":48},{"x":7,"y":3,"z":21},{"x":6,"y":7,"z":42}]
    }
  ]
 }

任何想法如何实现...... ????

--------更新了问题------

for (int k = 0; k < 4; k++)
{
  List<HMData> Data_Content = new List<HMData>();
   for (int l = 0; l < 7; l++)
    {  
      Value_LfromList = LValues.ElementAt((k * 7) + l);
      Value_IfromList = IValues.ElementAt((k * 7) + l);
      Value_BfromList = BValues.ElementAt((k * 7) + l);
       Data_Content.Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
     }
     data_list.Add(Data_Content);
  } 
  var chart = new
        {
            type = ChartType
        };
   var data = new { data=data_list.ToArray() };
   var series = new[] { data };
   var obj = new {chart,series};
   string result = jSearializer.Serialize(obj);

但我得到的输出仍然相同,如下所示,

{"chart":{"type":"bubble"},"series":[{"data":[[{"x":7,"y":7,"z":49},
{"x":7,"y":7,"z":49},{"x":7,"y":9,"z":63},{"x":5,"y":9,"z":45},{"x":4,"y":3,"z":12},
{"x":2,"y":6,"z":12},{"x":3,"y":5,"z":15}],[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
{"x":7,"y":8,"z":56},{"x":9,"y":6,"z":54},{"x":5,"y":7,"z":35},{"x":3,"y":8,"z":24},
{"x":4,"y":3,"z":12}],[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
{"x":8,"y":7,"z":56},{"x":5,"y":7,"z":35},{"x":3,"y":7,"z":21},{"x":5,"y":8,"z":40}],
[{"x":3,"y":7,"z":21},{"x":3,"y":7,"z":21},{"x":5,"y":2,"z":10},{"x":5,"y":2,"z":10},
{"x":8,"y":6,"z":48},{"x":7,"y":3,"z":21},{"x":6,"y":7,"z":42}]]}]}

1 个答案:

答案 0 :(得分:1)

试试这个:

System.Collections.Generic.List<object> dataList = new System.Collections.Generic.List<object>();
for (int k = 0; k < 4; k++)
        {
            List<HMData> Data_Content = new List<HMData>();
            for (int l = 0; l < 7; l++)
            {

                Value_LfromList = LValues.ElementAt((k * 7) + l);
                Value_IfromList = IValues.ElementAt((k * 7) + l);
                Value_BfromList = BValues.ElementAt((k * 7) + l);
                Data_Content.Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
            }
            dataList.Add(new {data = Data_Content});
        } 
  var chart = new
        {
            type = ChartType
        };
var series = dataList;
var obj = new { chart, series };
string result = jSearializer.Serialize(obj);