Question

Below is my main list

var serie_line = new { name = series_name, data = new List<object>() };

here I add items in data as follows,

serie_line.data.Add(child_object_name); 
serie_line.data.Add(period_final_value );

I then add this var serie_line to another list series as follows,

List<object> series = new List<object>(); 
series.Add(serie_line);

finally ,I serialize this series into JSON as below,

var obj4 = new { legend = legend, title,chart, series};
JSON_File_Returned = jSearializer.Serialize(obj4);

whereas

System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();

Now Output I am getting is as follows,

 {
    "legend":{"enabled":"true"},
    "title":{"text":"Financial"},
    "chart":{"type":"pie"},
    "series":[
               {"name":"Actual","data":["Market Share",20.00]},
               {"name":"Actual","data":["Sales Growth",30.00]},
               {"name":"Actual","data":["Operating Profit",40.00]},
               {"name":"Actual","data":["Gross Margin %",10.00]}
             ]
  }

But my required output is as follows,

 {
    "legend":{"enabled":"true"},
    "title":{"text":"Financial"},
    "chart":{"type":"pie"},
    "series":[
               {"name":"Actual","data":[["Market Share",20.00],["Sales Growth",30.00],["Operating Profit",40.00],["Gross Margin %",10.00]]}
             ]
  }

So..That I can plot pie chart in highchart using this JSON output...I have tried for everything like using dictionary,making different class and then using it's object and so on...but can't make it out....

Below is my entire code...if in case I am messing with any loop and I don't recognize it but any one might notice it..please check the below code for the same..

    var serie_line = new { name = series_name, data = new List<object>() };  

    for (int k = 0; k <= read_Series_Splitted_Name.Length; k++)                                 //for loop for passing chart type in series
        {
            for (int i = 0; i < id_series_before_offset.Count; i++)                                 //for loop for counting series ID
            {
                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", "");
                    new_series_name = series_name;
                            series_Atribute = Client.GetAttributeAsString(sessionId, resulted_series_id, "symbol", "");
                    if (read_Series_Splitted_Name_store == series_Atribute)
                    {
                       serie_line = new { name = series_name, data = new List<object>() };
                     }

                     k++;
                            //  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 && a == 0)
                      {
                          var periodid = xmlAttributeCollection_for_period["periodid"];
                          xmlActions[j] = periodid.Value;
                          period_final_id = periodid.Value;
                          period_name = Client.GetAttributeAsString(sessionId, periodid.Value, "name", "");
                          period_Name.Add(period_name);
                      }

                      try
                      {
                            var action = xmlAttributeCollection_for_period["value"];  //    xmlActionsone[j] = action.Value;
                            period_final_value = float.Parse(action.Value);
                                            // serie_line.data.Add(period_final_value);
                             serie_line.data.Add(child_object_name);
                             serie_line.data.Add(period_final_value );


                       }
                         catch (Exception ex1)
                       {
                             serie_line.data.Add("");
                             serie_line.data.Add( null );
                       }
                     }
                   }
                }
              }         
            }
           series.Add(serie_line);
Était-ce utile?

La solution

Your C# code should look something like this all stripped down:

var serie_line = new { name = "Actual", data = new List<object>() };
serie_line.data.Add(new List<object>() {"Market Share", 20.0});
serie_line.data.Add(new List<object>() {"Sales Growth", 30.0});
serie_line.data.Add(new List<object>() {"Operting Profit", 40.0});
serie_line.data.Add(new List<object>() {"Gross Margin %", 10.0});
jSearializer.Serialize(serie_line);

Which produces:

    {"name":"Actual","data":[["Market Share",20],["Sales Growth",30],["Operting Profit",40],["Gross Margin %",10]]}

I'm not following the bottom part of the code (how you create child_object_name and period_final_value, but I think you want:

    serie_line.data.Add(new List<object>() {child_object_name, period_final_value });

Instead of:

    serie_line.data.Add(child_object_name);
    serie_line.data.Add(period_final_value );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top