0

Now I have this class

public class FooResult
{
    public int sEcho;
    public Foo[] aaData;
    internal FooResult()
    {
        sEcho = 1;
        aaData = new FooRepository().GetAll().ToArray();
    }
}

new FooRepository().GetAll().ToArray(); returns an array of Foos.

When I use System.Web.Mvc.Controller.Json to convert this FooResult into a JsonResult, I got the string in Json Format like this:

{"sEcho":3, "aaData":[{"Name":"BarName"},{"Name":"FooName"}]}

However, I want the aaData to be a two dimensional array instead of an array of objects, which means it should be in this format:

{"sEcho":3, "aaData":[["BarName"],["FooName"]]}

How can I do that?

3
  • What you want is invalid JSON. Not sure what you are expecting Commented Jan 21, 2014 at 11:24
  • Sorry, I missed some commas. Here is the data format I want Commented Jan 21, 2014 at 11:26
  • The commas were an obvious mistake. The invalidity is within the array itself. You can't have ["key":"value"]. You can have `["key","value"] is you want. Commented Jan 21, 2014 at 11:28

1 Answer 1

1

for this case you can use something like this

internal FooResult()
{
    sEcho = 1;
    aaData = new FooRepository().GetAll()
                                .Select(foo=>new object[]{
                                    foo.Name
                                    /*other fields what need*/
                                 })
                                .ToArray();
}

OR you can change settings for plugin DataTables and send array of objects

NOTE
Change type aaData from Foo[] to object[][]

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.