I am new to C#. I am trying to convert the JSON string into a list looks like this:
firstName lastName departmentID
John Doe AAA111
John2 Doe2 BBB222
Updated Post - based on David's answer (Thank you for that). Sorry for all the confusion I might have caused.
Here is the code:
public IActionResult JsonTest()
{
var respJSON = @"
[
{
""firstName"": ""John"",
""lastName"": ""Doe"",
""departmentID"": { ""id"": ""AAA111"" },
},
{
""firstName"": ""John2"",
""lastName"": ""Doe2"",
""departmentID"": { ""id"": ""BBB222"" }
}
]
";
List<UserNameWithDept> user = JsonConvert.DeserializeObject<List<UserNameWithDept>>(respJSON);
return Ok();
}
public class Identifier
{
[JsonProperty("id")]
public string Id { get; set; }
}
public class UserNameWithDept
{
[JsonProperty("firstName")]
public string FirstName { get; set; }
[JsonProperty("lastName")]
public string LastName { get; set; }
[JsonProperty("departmentID")]
public Identifier Department { get; set; }
}
I am getting the Department as an object, then value inside it.

How can I get the list look this? Department has value, no an object.
Department "AAA111"
FirstName "John"
LastName "Doe"
}from thedepartmentIDproperty.}and it worked fine. Sorry, I am not sure that I understand.