I have a .NET Core 1.x Web API project and I am trying to accept an array on HTTP post, but I have been unable to get this working. I have validated the following JSON
{
"LogEntry": [{
"a": 1238976,
"b": "test",
"c": "sub test",
"d": "some syb system comp",
"e": 1234,
"f": "my category",
"g": "my event name",
"h": "my sub event",
"i": "user def 1",
"j": "7/22/2008 12:11:04 PM",
"k": 45,
"l": 65,
"n": "Chris",
"o": "C:\\temp\\",
"p": 1234567890,
"q": 84,
"r": "eeeee stuff",
"s": "ddddd stuff",
"t": 90210
}]
}
I have a model class so I need each array item to be added to a list of the model type. This is where I am stuck. I have only been able to get this working with a single entry not in an array. My C# for that scenario is:
[HttpPost]
public string Post([FromBody] JObject test)
{
var result = JsonConvert.DeserializeObject<EventManagerLogEntry>(test.ToString());
return "wootwoot";
}
Any direction on how I can loop through each array in my jObject and have it added to a list of type would be very helpful.
Class Definition
public class EventManagerLogEntry
{
public int a { get; set; }
public string b { get; set; }
public string c { get; set; }
public string d { get; set; }
public int e { get; set; }
public string f { get; set; }
public string g { get; set; }
public string h { get; set; }
public string i { get; set; }
public string j { get; set; }
public int k { get; set; }
public int l { get; set; }
public string m { get; set; }
public string n { get; set; }
public int o { get; set; }
public int p { get; set; }
public string q { get; set; }
public string r { get; set; }
public int s { get; set; }
}
UPDATE I tried several different methods and this seems to be working for me.
[HttpPost]
public HttpResponseMessage Post([FromBody] JArray test)
{
var list = JsonConvert.DeserializeObject<List<EventManagerLogEntry>>(test.ToString());
foreach (EventManagerLogEntry x in list)
{
//_context.EventManagerLogEntry.Attach(x);
//_context.SaveChanges();
}
return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
}
var resulListt = JsonConvert.DeserializeObject<List<EventManagerLogEntry>>(test.ToString());