以必需的JSON格式从列表中获取数据

时间:2014-01-07 07:35:27

标签: c# 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个数据对象存储在一个数据字符串中,我希望有不同的数据字符串......任何想法我都能做到这一点,

1 个答案:

答案 0 :(得分: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);

您解决方案中的问题在于,为了获得您想要的结果,您需要将“数据”与每个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

希望这会有所帮助。 欢呼声。