I am querying a table with C# through a dataAccess
, it returns a dataTable
and I parse them in a webMethod
JSON format and return the response in this format:
"datos": [
{
"idPermiso": 1,
"nombre": "Catálogos",
"idUsuario": 11478,
"email": "labm"
},
{
"idPermiso": 2,
"nombre": "Remediación",
"idUsuario": 11478,
"email": "labm"
},
{
"idPermiso": 1,
"nombre": "Catálogos",
"idUsuario": 12338,
"email": "BGARCIA"
},
{
"idPermiso": 2,
"nombre": "Remediación",
"idUsuario": 12338,
"email": "BGARCIA"
}
]
They are fine, but I would like to reorder them as follows to avoid duplicating unnecessary data such as user ID or email.
"datos": [
"user": {
"idUsuario": 12338,
"email": "BGARCIA"
"permisos": [
{
"idPermiso": 1,
"nombre": "Catálogos"
},
{
"idPermiso": 2,
"nombre": "Catálogos 2"
}
},
"user": {
"idUsuario": 12338,
"email": "LABM"
"permisos": [
{
"idPermiso": 1,
"nombre": "Catálogos"
}
}
]
I've been trying with dictionaries but I still can't get the format I want, can you give me an idea?
I also attach the WebMethod in which I return the JSON, you will see that I am trying to do this with dictionaries, I am currently playing with them.
Note I only want to do this formatting in the if(userid == -1) part, the else part is fine as is.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static RespuestaJS ObtenerPermisosXUsuario(int idUsuario)
{
RespuestaJS respuestaJS = new RespuestaJS();
try
{
DataAccessPermiso dataAccess = new DataAccessPermiso();
RespuestaBD respuestaBD = new RespuestaBD();
List<object> usuarioPermisosLista = new List<object>();
respuestaBD = dataAccess.ObtenerPermisosXUsuario(idUsuario);
List<object> permisos = new List<object>();
int temp = 0;
if (respuestaBD.transaccion == 1)
{
if (idUsuario == -1)
{
var arg = new Dictionary<string, List<object>>();
var data = new Dictionary<string, object>();
foreach (DataRow fila in respuestaBD.datos.Rows)
{
int idUser = (int)fila["IdUsuario"];
if (idUser == temp)
{
object permisoItem = new
{
idPermiso = fila["nidPermiso"],
nombre = fila["sNombre"]
};
permisos.Add(permisoItem);
arg.Add("user", permisos);
data.Add("data", arg);
}
temp = idUser;
};
respuestaJS.transaccion = RespuestaJS.EXITO;
respuestaJS.datos = arg;
respuestaJS.mensaje = "ok";
}
else
{
for (int i = 0; i < respuestaBD.datos.Rows.Count; i++)
{
DataRow fila = respuestaBD.datos.Rows[i];
object permisoItem = new
{
idPermiso = fila["nidPermiso"],
nombre = fila["sNombre"],
idUsuario = fila["IdUsuario"],
email = fila["Email"]
};
usuarioPermisosLista.Add(permisoItem);
}
respuestaJS.transaccion = RespuestaJS.EXITO;
respuestaJS.datos = usuarioPermisosLista;
respuestaJS.mensaje = "ok";
}
}
else
{
respuestaJS.transaccion = RespuestaJS.ERROR;
respuestaJS.mensaje = "Error al consultar o ningún resultado coincide";
respuestaJS.datos = null;
}
}
catch (Exception ex)
{
respuestaJS.transaccion = RespuestaJS.ERROR;
respuestaJS.datos = null;
respuestaJS.mensaje = ex.Message;
}
return respuestaJS;
}
I think something like this could work for you. It's basically grouping with LinQ by UserID.
I have used Newtonsoft to facilitate the notation in
Json
:You can see the example here: https://dotnetfiddle.net/mDv0eF
*The Dump() function is from dotnetfiddle and I use it to "dump" the variable in the output of the program.