Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环浏览列表并将其序列化';将单个数据转换为JSON_C#_Json - Fatal编程技术网

C# 循环浏览列表并将其序列化';将单个数据转换为JSON

C# 循环浏览列表并将其序列化';将单个数据转换为JSON,c#,json,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.JavaScriptSer

下面是我获取值的代码

    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","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_名称和p_值的列表进行循环
我无法获得解决方案,任何帮助都将不胜感激,

请尝试以下方法。为简便起见,将其最小化:

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);
List modified_listofstrings=new List();
列表系列=新列表();
System.Web.Script.Serialization.JavaScriptSerializer jSearializer=新的System.Web.Script.Serialization.JavaScriptSerializer();
对于(int i=0;i
非常感谢,我在连接两个不同的列表和两个不同数据类型的列表时遇到问题..这正是我想要的…再次感谢您,您给出了这个解决方案var serie=new{name=series\u name,data=new list()};。。。但是,如果我需要在数据列表中添加字符串和浮点,您能给我一个解决方案吗…因为我在向特定列表添加多个数据类型时遇到了困难…我需要一些变量serie=new{name=series\u name,data=new list()};。。。但这不管用
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);