MojoDK
MojoDK

Reputation: 4528

Json compress table?

In my MVC project, I return like 300 rows, which has exact same structure (fields), so instead of this:

[{
  name : "John",
  age : 11,
}, {
  name : "Jane",
  age : 21,
}, {
  name : "Poul",
  age : 18,
}]

Is it possible, in vb.net (or c#), to only state the fieldnames once and return like this:

[["name","age"],["John",11],["Jane",21],["Poul",18]]

That would save me like 50% of the code returned from server to client.

Upvotes: 1

Views: 152

Answers (1)

abc123
abc123

Reputation: 18763

yes, return an object[][] in .Net and make your inner array contain only the values.

Example:

public object[][] GetUsers()
{
    List<object[]> users = new List<object[]>();
    //Get users and store them in variable called RealUsers or cycle through DataRows
    foreach(User user in RealUsers)
    {
        users.add(new object[]() {user.Name, user.Age});
    }

    return users.ToArray();
}

Upvotes: 1

Related Questions