2

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;
 }
Roberto
  • 504
  • 11
  • 23
  • 1
    why do you want it that way.. Order does not matter in JSON – Sushanth -- Sep 27 '12 at 08:30
  • because: - I need to print out the data in a specific order - I dont' want to add a order field - html/js does not know nothing about the data. Just print each field name and corresponding value – Roberto Sep 27 '12 at 09:44
  • A work-around could be to hard code in the field name the order number (e.g. Id_1, LegsNumber_2, NeckLength_3) and use it to sort the results but is not very elegant – Roberto Sep 27 '12 at 13:33

2 Answers2

2

Try attributing your fields with JsonPropertyAttribute like this:

public abstract class Animal
{
   [JsonProperty(Order = 0)]
   public string Id { get; set; }

   [JsonProperty(Order = 1)]
   public int LegsNumber { get; set; }
}

public class Giraffe: Animal
{
   [JsonProperty(Order = 2)]
   public double NeckLength { get; set; }
}
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Right! This is what I need. Unfortunately it does not affect the behaviour of JsonResult class, however i substitute it with Newtonsoft.Json.JsonConvert and works like a charm. Thanks – Roberto Sep 28 '12 at 09:59
  • The only problem is that is a bit inefficient: From RavenDb (json) serialize results in POCO and than deserialize them in json again. I think there is a straightforward way but this is a complete different question. – Roberto Sep 28 '12 at 10:03
  • Take a look here: http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx – Matt Johnson-Pint Sep 28 '12 at 19:45
0

Json is not providing an index based access. Instead you can try something like this

for (int i = 0; i < Data.length(); i++) {
 var item=Data.[i];

 var row = '<tr>';
  row += '<td><b>' +item['Id'] + '</b></td>'+'<td><b>' +item['LegsNumber'] + '</b></td>'
 +item['NeckLength'] + '</b></td>'+'<tr>';
}
amesh
  • 1,311
  • 3
  • 21
  • 51
  • No index based access Ok. But it gives the list of fields. Is there a way to change the order of the field list? Your solution does not work for me because the script must works with any Json structure without knowing the names of the fields – Roberto Sep 27 '12 at 13:30
  • 1
    Json is not maintaining order. Still if you want to maintain order, add one more integer field to each item which contain the sort order, and write some custom javascript sort method. – amesh Sep 27 '12 at 14:17