0

Below is my code to get the JSON data,

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 chart = new
        {
            type = ChartType
        };
   var data = new { data=data_list };
   var series = new[] { data };
   var obj = new {chart,series};
   string result = jSearializer.Serialize(obj);

Output I get is as follows,

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

but I want the output as follows,

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

Any Idea how it can be achieved...????

--------Updated question------

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 chart = new
        {
            type = ChartType
        };
   var data = new { data=data_list.ToArray() };
   var series = new[] { data };
   var obj = new {chart,series};
   string result = jSearializer.Serialize(obj);

but output I am getting is still the same,as follows,

{"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}]]}]}
8
  • have you tried to declare data as an array instead of a list? Commented Jan 6, 2014 at 13:27
  • Which list you are talking about...?...data_list or var data..??? Commented Jan 6, 2014 at 13:29
  • but then I get error since I am not able to add the data_content list value to array of data..can you show me how can I assign values to array,it will be very grateful of you Commented Jan 6, 2014 at 13:38
  • you can convert it to array: var data = new { data=data_list.ToArray() } Commented Jan 6, 2014 at 13:42
  • if you mean to say that I should define var data in above code the way you have given but still I cannot get desired output, Commented Jan 6, 2014 at 13:48

1 Answer 1

1

Try this:

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 chart = new
        {
            type = ChartType
        };
var series = dataList;
var obj = new { chart, series };
string result = jSearializer.Serialize(obj);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.