循环遍历列表并将其单个数据序列化为JSON

时间:2013-11-19 12:54:51

标签: c# json

以下是获取值的代码,

    List<object> modified_listofstrings = new List<object>();
    List<string> p_Name = new List<string>();
    List<string> s_Name = new List<string>();
    System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();

for (int i = 0; i < id_series_before_offset.Count; i++)
            {
                var xmlAttributeCollection = id_series_before_offset[i].Attributes;
                if (xmlAttributeCollection != null)
                {
                    var seriesid = xmlAttributeCollection["id"];
                    xmlActions_id[i] = seriesid.Value;
                    resulted_series_id = seriesid.Value;
                    series_name = Client.GetAttributeAsString(sessionId, resulted_series_id, "name", "");
                    s_Name.Add(series_name);----// Contains Target,Alarm,Actual

                    //  Forloop for PeriodId and It's Value

                    var value = Read_XML_Before_Offset.SelectNodes("//measure.values/series[" + (i + 1) + "]/value");
                    var xmlActions = new string[value.Count];// for periodname
                    var xmlActionsone = new string[value.Count];  // for period value

                    for (int j = 0; j < value.Count; j++)
                    {

                        var xmlAttributeCollection_for_period = value[j].Attributes;
                        if (xmlAttributeCollection_for_period != null)
                        {

                            if (i == 0)
                            {
                                var periodid = xmlAttributeCollection_for_period["periodid"];
                                xmlActions[j] = periodid.Value;
                                period_final_id = periodid.Value;
                                period_name = Client.GetAttributeAsString(sessionId, periodid.Value, "name", "");
                                p_Name.Add(period_name);
                            }
                            var action = xmlAttributeCollection_for_period["value"];
                            xmlActionsone[j] = action.Value;
                            period_final_value = float.Parse(action.Value);
                            p_Value.Add(period_final_value);

                        }
                    }
                }
            }

                var obj_series = new
                {
                    name = s_Name,-------// I want here to have multiple series
                    data = p_Value------// values should belongs to respective series
                };
                var series = new[] { obj_series };

            var obj_categories = new
            {
                categories = p_Name
            };

            var xAxis = new[] { obj_categories };

            var chart = new
            {
                Type = read_ChartType
            };
            var obj_legend = new
            {
                layout,
                floating,
                backgroundColor,
                align,
                verticalAlign,
                y,
                x,
            };

            var legend = new[] { obj_legend };
            var obj4 = new { legend = new[] { obj_legend }, chart, series, xAxis };
            modified_listofstrings.Add(obj4);
            jSearializer.Serialize(modified_listofstrings);

,我得到的输出是,

  {
  "legend":[{"layout":"vertical","floating":"true","backgroundColor":"#FFFFFF","align":"right",
"verticalAlign":"top","y":"60","x":"-60"}],
  "chart":{"Type":"line"},
  "series":[{"name":["01. Target","02. Alarm","03. Actual"],
  "data":[14,14,14,14,18,18,18,18,17,15,13,12]}],
  "xAxis":[{"categories":["Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013"]}]}

但我期望的数据如下,

  {"legend":  [{"layout":"vertical","floating":"true","backgroundColor":"#FFFFFF","align":"right","verticalAlign":"top","y":"60","x":"-60"}],
 "chart":{"Type":"line"},
 "series":[{"name":"01. Target","data":[14,14,14,14]},{"name":"02. Alarm","data":[18,18,18,18]},{"name":"03. Actual","data":[17,15,13,12]}],
"xAxis":[{"categories":["Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013"]}]}

如何循环遍历s_Name和p_Value列表 我无法得到解决方案,我们将非常感谢任何帮助,

1 个答案:

答案 0 :(得分:1)

尝试以下方法。它简化了最小化:

List<object> modified_listofstrings = new List<object>();
List<object> series = new List<object>();
System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();

for (int i = 0; i < id_series_before_offset.Count; i++)
{
    var xmlAttributeCollection = id_series_before_offset[i].Attributes;
    if (xmlAttributeCollection != null)
    {
        ...
        series_name = QPR_webService_Client.GetAttributeAsString(sessionId, resulted_series_id, "name", "");
        var serie = new {name = series_name, data = new List<float>()};
        ...

        for (int j = 0; j < value.Count; j++)
        {
            ...
            if (xmlAttributeCollection_for_period != null)
            {
                ...
                period_final_value = float.Parse(action.Value);
                serie.data.Add(period_final_value);
            }
        }

        series.Add(serie);
    }
}
...

var legend = new[] { obj_legend };
var obj4 = new { legend = new[] { obj_legend }, chart, series, xAxis };
modified_listofstrings.Add(obj4);
jSearializer.Serialize(modified_listofstrings);