0

i have this function in VB.NET

Dim dt = (From d In db.d ...).ToArray()

Return New With {
    .label = "label",
    .data = dt.ToArray()
}

that returns the following data in JSON format

{
    "label":"label",
    "data":[
        {"date":1366657400000,"value":12051},
        {"date":1366657397000,"value":12102},
        {"date":1366646820000,"value":12099}
    ]
}

How can I go about returning the data in the following format

{
    "label":"label",
    "data":[
        [1366657400000,12051],
        [1366657397000,12102],
        [1366646820000,12099]
    ]
}
2
  • Are you sure you want to do this? The original JSON looks better to me. Commented Apr 22, 2013 at 20:37
  • @svick I agree, but I need to do so in order for Flot plugin to work! Commented Apr 23, 2013 at 21:30

1 Answer 1

1

I'm not very familiar with VB, but it should look something like this:

Return New With { _
    .label = "label", _
    .data = data.[Select](Function(d) New Long() { d.date, d.value} ).ToArray() _
}

The idea is that you want to create a new array for each item in your original collection. The first item in the new array is the date property, and the second item is the value property.

This is what it would look like in C#:

return Json(new {
    label = "label",
    data = data.Select(d => new[] { d.date, d.value }).ToArray()
});
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, that doesn't compile. I tried Return New() With { .label = "label", .data = data.ToArray() } but it gives me a syntax error 'Type expected'
I know how to write it in c#, I found a similar answer here but I don't know how to convert d => new[] to VB. The syntax New() With doesn't compile.
Write new string() in place of new[], to start with - assuming its going to be a string array.
@Elio - Sorry... as I said, I don't know VB very well. But the method is correct: create a new array from each item in the original data collection. That way, an array of arrays is serialized, and you should get your desired output.
@Elio - I fixed the VB example. It now compiles and works for me.
|

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.