How to return JSON not associative array

c# c#-4.0 dapper json.net servicestack

Question

I'm writing simple API which returns array of JSON objects:

[
    {
        "Id": 134649,
        "Type_id": 6,
        "Latitude": 56.904220,
        "Longitude":14.823440
    },
    {
        "Id": 134660,
        "Type_id": 6,
        "Latitude": 56.884040,
        "Longitude":14.761320
    }
]

This is generated by Response.MapEntries model I wrote:

class MapEntries
{
    public ulong Id { get; set; }
    public int Type_id { get; set; }
    public decimal Latitude { get; set; }
    public decimal Longitude { get; set; }
}

And filled and returned like this:

List<Response.MapEntries> entries = new List<Response.MapEntries>();
using (IDbConnection db = Connection.Instance())
{
    db.Open();
    entries = db.Query<Response.MapEntries>(query.ToString(), parameters).ToList();
}

return entries;

PROBLEM

JSON response has useless information like JSON object's names. By one request it can return up to 20000(1.2MB) records (demo image: http://i.imgur.com/67dsPuh.jpg).

I think I can save around 40% of data transfer, if I change JSON to not associative arrays. But I don't know how to do that, because I'm new with C# and strict typed languages.

Response I want: [[134649, 6, 56.884040, 14.761320],[134649, 6, 56.884040, 14.761320]]

Accepted Answer

As you are mixing data types, return each item as an array of objects:

List<object[]> entries;
using (IDbConnection db = Connection.Instance()) {
  db.Open();
  entries = db.Query<Response.MapEntries>(query.ToString(), parameters)
    .Select(e => new object[] { e.Id, e.Type_id, e.Latitude, e.Longitude })
    .ToList();
}

return entries;



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why