我正在编写简单的API,它返回JSON对象的数组:
[
{
"Id": 134649,
"Type_id": 6,
"Latitude": 56.904220,
"Longitude":14.823440
},
{
"Id": 134660,
"Type_id": 6,
"Latitude": 56.884040,
"Longitude":14.761320
}
]
这是由我写的Response.MapEntries
模型生成的:
class MapEntries
{
public ulong Id { get; set; }
public int Type_id { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}
并填写并返回如下:
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;
问题
JSON响应具有无用的信息,如JSON对象的名称。根据一个请求,它可以返回高达20000(1.2MB)的记录(演示图像: http : //i.imgur.com/67dsPuh.jpg )。
如果我将JSON更改为非关联数组,我想我可以节省大约40%的数据传输。但我不知道该怎么做,因为我是C#和严格类型语言的新手。
我想要的回应: [[134649, 6, 56.884040, 14.761320],[134649, 6, 56.884040, 14.761320]]
在混合数据类型时,将每个项目作为对象数组返回:
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;