I'm having problems when trying to separate a JSON into different values, how can I separate the json to save them in variables and be able to display them in an html input? I am using Axis.
this is the json i get;
obtenerUsuario:
apell_USUARIO: "pumbaa"
contra_USUARIO: "1234"
fec_REG_USUARIO: "2020-10-22T11:38:14.383"
id_USUARIO: 2
nom_USUARIO: "timon"
usu_USUARIO: "local2"
this is the jquery axios, the data is stored in rpta and that is where I want to separate it
function fnObtener(){
var id= $('#txtId').val();
axios.post('http://localhost:8080/api/usu/obtenerUsuario', {
"ID_USUARIO": id
}, {
headers: {
'Content-Type': 'application/json',
}
}, {
}).then((response) => {
fnRespuestaObtener(response.data);
})
}
function fnRespuestaObtener(rpta){
var id= $('#txtId').val();
var nombre=$('#txtNombres').val();
var apellido=$('#txtApellidos').val();
var usuario=$('#txtUsuario').val();
var contrasea=$('#txtContrasena').val();
if(id=="ID_USUARIO"){
nombre.text=nom_USUARIO;
apellido.text=apell_USUARIO;
usuario.text=usu_USUARIO;
contrasea.text=contra_USUARIO;
}
}
If I understand you correctly, by separating the json to store it in variables, you mean destructuring JSON objects.
When you do the
post
with Axios and get an object as a response, you can destruct it to access its properties.If the item you get consists of:
Then you can implement the destructuring in
fnRespuestaObtener()
like this:Dear, I explain how you can do it, in this case I use a public json api, in your case it uses yours, but the operation is the same.
first you have to know how your request arrives
axios
that's why I asked you for the console.log, but I think it's the first one, in any case you would have to show it as I do it inside thefnRespuestaObtener
, so that you can fill in your input.PDTA. Use
Jquery
to fill the input, but you could do it withJavascript
, let's use both as an example.I would do something like this:
For each data returned in the JSON it is a cycle, when indicating that the cycle is carried out in "rpta.obtenerUsuario" it will search in this how many records exist, and since there is only one it will set them' the values found with the specific aliases that indicate.