Ernestas Stankevičius
Ernestas Stankevičius

Reputation: 2493

How to return JSON not associative array

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: https://i.sstatic.net/Corw5.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]]

Upvotes: 3

Views: 940

Answers (1)

Guffa
Guffa

Reputation: 700152

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;

Upvotes: 5

Related Questions