I have a model stored with RavenDB done in this way:
public abstract class Animal
{
public string Id { get; set; }
public int LegsNumber { get; set; }
}
public class Giraffe: Animal
{
public double NeckLength { get; set; }
}
In my MVC controller I query all the Giraffe and put the result in Json format in this way:
return new JsonResult
{
JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet,
Data = DocumentSession.Query<Giraffe>()
};
The output is something like
[{"NeckLength":2.5, "Id":"bob", "LegsNumber":4}, {...}, ...]
How can I customize the field order in order to have a result like this:
[{"Id":"bob", "LegsNumber":4, "NeckLength":2.5}, {...}, ...]
?
If you are curious and you want to know why I want to change the order is because I have a generic javascript snippet that take the json and print it out as it is:
for (var field in data[0]) {
var row = '<tr>';
row += '<td><b>' + field + '</b></td>'
$.each(data, function (i, value) {
row += '<td>' + value[field] + '</td>'
});
row += '</tr>';
result += row;
}