1

Below is my code which gives 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 data = new{data=data_list};
var series = new []{ data};
var obj = new { chart, series };
string result = jSearializer.Serialize(obj);

output I am getting 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 data to be 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}]}
    ]
}

In short,my 4 data object get store in one data string ,I want to have different data string ...Any idea how I can do this,

1 Answer 1

2

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 series = dataList;
var obj = new { chart, series };
string result = jSearializer.Serialize(obj);

The problem in your solutions lies in the fact that in order to get te result you want, you need to associate "data" to each Data_Content (from each inner iteration).

Therefore, changing it this way, in dataList you will get the equivalent of this:

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

Then we assign this to series so as to get that key for the whole list, and then add your chart and series objects to the final obj which you then serialize.

Hope this helps. Cheers.

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.