I have this code
return Ok(
new object[]
{
new { name = Dname, primaryLastName =DprimaryLastName, secondLastName= DsecondLastName,degrees= Ddegrees, roles= Droles, entityId = DentityId, enrollment = Denrollment, payrollNumber = DpayrollNumber, photo = Dphoto, groupsInfo = rt , birthDate = DbirthDate}
}
);
All variables values are string, except rt:
var rt = new Dictionary<string, string>();
foreach (DataTable table in dsgroupsinfo.Tables)
{
foreach (DataRow dr in table.Rows)
{
rt.Add("required", dr["requerido"].ToString());
rt.Add("optional", dr["opcional"].ToString());
}
}
My result from the service is this:
[
{
"name": "string",
"primaryLastName": "string",
"secondLastName": "string",
"degrees": "string",
"roles": "string",
"entityId": "string",
"enrollment": "string",
"payrollNumber": "string",
"photo": "string",
"groupsInfo": {
"required": "string",
"optional": "string"
},
"birthDate": "string"
}
]
how can I get a response like this:
{
"name": "string",
"primaryLastName": "string",
"secondaryLastName": "string",
"degrees": "string",
"roles": "string",
"entityId": "string",
"enrollment": "string",
"photo": "",
"groupsInfo": {
"required":["string"],
"optional": ["string"]
}
}
Please note there is no square brackets in the begining and the end, and inside groups info there are square brackets.
Hope you can help me, thanks.
return Ok(new { name = Dname, .....});..."required":["string"],...- why would you want that? How is that a string array? It is not and never will be.(rt.TryGetValue("required", out var list) ? list : rt["required"] = new List<string>()).Add(dr["requerido"].ToString());the dictionary needs to be declaredDictionary<string, List<string>>. Ideally you would just create a proper object model for your JSON, rather than using anonymous types and dictionaries