CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Dynamic;
using AutoMapper;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
MapperConfiguration _config = new MapperConfiguration(config =>
{
config.CreateMap<Source, List<Destination>>()
.ConvertUsing<SourceToListDestinationTypeConverter>();
});
string json = @"{
""Request"": {
""RequestedSymbols"": ""ABC,BCD,CDE"",
""RequestedFields"": ""ASK,BID,LAST"",
""Host"": ""XYZ"",
""Id"": ""123""
},
""Error"": {
""Code"": ""0"",
""Description"": """"
},
""Records"": {
""Record"": [
{
""Fields"": {
""Field"": [
{
""Id"": ""200"",
""Name"": ""ASK"",
""Value"": ""43.75""
},
{
""Id"": ""100"",
""Name"": ""BID"",
""Value"": ""43.73""
},
{
""Id"": ""300"",
""Name"": ""LAST"",
""Value"": ""43.73""
}
]
},
""Req_sym"": ""ABC"",
""Key"": ""ABC"",
""Stale"": """"
},
{
""Fields"": {
""Field"": [
{
""Id"": ""200"",
""Name"": ""ASK"",
""Value"": ""76.18""
},
{
""Id"": ""100"",
""Name"": ""BID"",
""Value"": ""76.17""
},
{
""Id"": ""300"",
""Name"": ""LAST"",
""Value"": ""76.19""
}
]
},
""Req_sym"": ""BCD"",
""Key"": ""BCD"",
""Stale"": """"
},
{
""Fields"": {
""Field"": [
{
""Id"": ""200"",
""Name"": ""ASK"",
""Value"": ""16.34""
},
{
""Id"": ""100"",
""Name"": ""BID"",
""Value"": ""16.33""
},
{
""Id"": ""300"",
""Name"": ""LAST"",
""Value"": ""16.33""
}
]
},
""Req_sym"": ""CDE"",
""Key"": ""CDE"",
""Stale"": """"
}
]
},
""Service"": ""XYZ""
}";
Source src = JsonConvert.DeserializeObject<Source>(json);
IMapper _mapper = _config.CreateMapper();
List<Destination> dests = _mapper.Map<Source, List<Destination>>(src);
Console.WriteLine(JsonConvert.SerializeObject(dests, new JsonSerializerSettings { Formatting = Formatting.Indented }));
}
}
public class Source
{
public Request Request { get; set; }
public Error Error { get; set; }
public Records Records { get; set; }
public string Service { get; set; }
}
public class Records
{
public List<Record> Record { get; set; }
}
public class Fields
{
public List<Field> Field { get; set; }
}
public class Field
{
public string Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class Request
{
public string RequestedSymbols { get; set; }
public string RequestedFields { get; set; }
public string Host { get; set; }
public string Id { get; set; }
}
public class Error
{
public string Code { get; set; }
public string Description { get; set; }
}
public class Record
{
public Fields Fields { get; set; }
public string Req_sym { get; set; }
public string Key { get; set; }
public string Stale { get; set; }
}
public class Destination
{
public string Symbol { get; set; }
public string ASK { get; set; }
public string BID { get; set; }
public string LAST { get; set; }
}
public class SourceToListDestinationTypeConverter : ITypeConverter<Source, List<Destination>>
{
public List<Destination> Convert(Source source, List<Destination> destination, ResolutionContext context)
{
List<Destination> result = new List<Destination>();
foreach (var record in source.Records.Record)
{
Dictionary<string, object> fieldDict = record.Fields.Field
.ToDictionary(x => x.Name, x => (object)x.Value);
fieldDict.Add(nameof(Destination.Symbol), record.Req_sym);
result.Add(context.Mapper.Map<Destination>(fieldDict));
}
return result;
}
}