How to produce JSON in the format that Highcharts expects?

Asked

Viewed 682 times

6

I have the following code in C#:

Dictionary<string, object> dataResult = new Dictionary<string, object>();
dataResult["data"] = new List<object[]>();
dataResult["name"] = i;
foreach (var item in query)
{
    object[] values = new object[2];
    values[0] = Convert.ToDecimal(item.ini) ;
    values[1] = Convert.ToDecimal(item.ir);
    ((List<object[]>)dataResult["data"]).Add(values);
}

With it I will present the following JSON:

  var data = [
         {"TESTE2008",23.4,2},
         {"TESTE2008",24.4,3},
         {"TESTE2008",25.4,4},
         {"TESTE2008",26.4,5},
         {"TESTE2008",27.4,6},
         {"TESTE2008",28.4,7},
         {"TESTE2008",29.4,8},
         {"TESTE2008",30.4,9},
         {"TESTE2009",30.4,2},
         {"TESTE2009",31.4,3},
         {"TESTE2009",32.4,4},
         {"TESTE2009",33.4,5},
         {"TESTE2009",34.4,6},
         {"TESTE2009",35.4,7},
         {"TESTE2009",36.4,8},
         {"TESTE2009",37.4,9},
];

and would like the data to be formatted as follows:

series: [{
                name: 'TESTE2008',
                data: [[23.4,2],[24.4,3],[25.4,4],[26.4,5],[27.4,6],[28.4,7],[29.4,8],[30.4,9]
]},
            {
                name: 'TESTE2009',
                data: [[30.4,2],[31.4,3],[32.4,4],[33.4,5],[34.4,6],[35.4,7],[36.4,8],[37.4,9]]},

        });

How to change my code to produce the desired output?

  • There are actually two errors there. 1. Your [tag:json] is being misgenerated. This [tag:json] that is in the question is not a valid [tag:json]. Every object in [tag:json] needs to be named. 2. [tag:highcharts] does not accept a array as a value if a given.

  • If you need two values in a single series, you actually need two different series.

  • I managed to put together a series, but I’m not getting more than one. I could check what is wrong with the script I put in this doubt: http://answall.com/questions/6019/varias-series-em-highchart-mvc-asp-net

  • Your json needs to be like this [{&#xA; name: 'TESTE2008',&#xA; data: [23.4,2,24.4,3,25.4,4,26.4,5,27.4,6,28.4,7,29.4,8,30.4,9]&#xA;}, {&#xA; name: 'TESTE2009',&#xA; data: [30.4,2,31.4,3,32.4,4,33.4,5,34.4,6,35.4,7,36.4,8,37.4,9]&#xA;}]

  • Got it, thanks for the help...

1 answer

8


Try something like that, I’m not good at but I think it will generate the way you need it

    /*
        [{ name: 'Huguinho', data: [7.0, 6.9, 9.5, 14.5, 18.2] },
        { name: 'Zezinho', data: [24.1, 20.1, 14.1, 8.6, 2.5] },
        { name: 'Luizinho', data: [17.9, 14.3, 9.0, 3.9, 1.0] }]
    */

    var objVet = new object[] { new { name = "Huguinho", data = new object[] { 7.0, 6.9, 9.5, 14.5, 18.2 } },
                                new { name = "Zezinho", data = new object[] { 24.1, 20.1, 14.1, 8.6, 2.5 } },
                                new { name = "Luizinho", data = new object[] { 17.9, 14.3, 9.0, 3.9, 1.0 } }};

    JavaScriptSerializer js = new JavaScriptSerializer();
    string strJson = js.Serialize(objVet);
    return strJson;
  • I changed the shape of the question so I could express my doubt better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.