2014-01-06 114 views
0

下面是我的代碼來獲取JSON數據獲取數據,要從列表成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}]]}]} 
+0

您是否嘗試將數據聲明爲數組而不是列表? –

+0

你正在談論哪個列表...?... data_list或var數據..? – Reshma

+0

我的意思是data_list。 –

回答

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);