2014-01-07 59 views
1

下面是我的代碼,這給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 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}]} 
    ] 
} 

總之,我的4個數據對象獲取存儲在一個數據串,我想有不同的數據串......任何想法浩W I可以做到這一點,

回答

2

試試這個:

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 series = dataList; 
var obj = new { chart, series }; 
string result = jSearializer.Serialize(obj); 

在您的解決方案的問題在於,爲了得到你想要的TE結果,你需要「數據」到每個Data_Content關聯的事實(來自每次內部迭代)。

因此,改變這種方式,在DataList控件你會得到這樣的等價物:

[ 
{"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}]} 
] 

然後我們給這系列從而得到整個列表鍵,然後添加您的系列對象最終OBJ你再序列化。

希望這會有所幫助。 乾杯。

+0

非常感謝...ü再次保存me..Thanks很多... – Reshma

相關問題