JSON.Net Serializing a collection into an array of arrays

By default, DataTables Ajax expects JSON data to be an array of arrays.

Using JSON.Net , you can serialize the collection into an array of arrays instead of an array of objects via an attribute / parameter or do it using a custom JsonConverter . The object I'm trying to serialize is as follows:

 public class DeviceIndex { public int Id { get; set; } [Display(Name = "Asset Tag")] public string AssetTag { get; set; } [Display(Name = "Lease End")] [DataType(DataType.Date)] public DateTime LeaseEndDate { get; set; } public string Type { get; set; } [Display(Name = "Operating System")] public string OperatingSystem { get; set; } public string Model { get; set; } } 

And my action with the controller is as follows:

 public IActionResult IndexJson() { var model = _db.Devices.Select(d => new DeviceIndex { ... }).ToList(); return Content(JsonConvert.SerializeObject(new { data = model }), "application/json"); } 

What JSON outputs:

 { "data": [ { "Id": 1649, ... }, { "Id": 703, ... } ] } 

Although my desired results are as follows:

 { "data": [ [ "1649", ... ], [ "703", ... ] ] } 

I thought I could use the JsonArray attribute for my collection, but didn't want to change the output.

 [JsonArray] public class DevicesIndex: IEnumerable<DeviceIndex> { List<DeviceIndex> devices; public IEnumerator<DeviceIndex> GetEnumerator() { return devices.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } 
+5
source share
2 answers

Custom JsonConverter be a suggested method

 public class DeviceIndexJsonConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(DeviceIndex); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) return; writer.WriteStartArray(); var properties = value.GetType().GetProperties(); foreach (var property in properties) writer.WriteValue(value.GetType().GetProperty(property.Name).GetValue(value)); writer.WriteEndArray(); } } 
0
source

You can just achieve this, just update your IndexJson() method using this simple example.

You need to serialize List<List<object>> , try this sample. This is a List<string> list.

 public IActionResult IndexJson() { List<List<string>> data = new List<List<string>>(); data.Add(new List<string>(new string[] {"test1", "test2"})); data.Add(new List<string>(new string[] {"test3", "test4"})); var json = JsonConvert.SerializeObject(new {data = data}); return Content(json, "application/json"); } 

You will get this JSON

 { "data":[ ["test1","test2"], ["test3","test4"] ]} 

Using a data model

  List<List<string>> data =new List<List<string>>(); List<DeviceIndex> deviceIndex = new List<DeviceIndex>(); DeviceIndex item = new DeviceIndex(); item.Id = 111; item.AssetTag = "Tag"; item.LeaseEndDate = DateTime.Now; item.Type = "Type"; item.OperatingSystem = "OS"; item.Model = "Model"; deviceIndex.Add(item); deviceIndex.Add(item); foreach (var device in deviceIndex) { data.Add(new List<string>(new string[] { device.Id.ToString(), device.AssetTag, device.AssetTag, device.OperatingSystem })); } var json = JsonConvert.SerializeObject(new {data= data }); 

Json

 { "data":[ ["111","Tag","Tag","OS"], ["111","Tag","Tag","OS"] ] } 
+2
source

Source: https://habr.com/ru/post/1260220/


All Articles