Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

c# - To loop through list and serialize it's individual data into JSON

Below is my code to get the values,

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

and the output I get is,

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

but what I expect data is as follows,

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

How can I get loop through list of s_Name and p_Value I am not able to get the solution,any help will be greatly appreciated,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try the following. Its minimized for simplicity:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...