简体   繁体   中英

Access specific data on Json - NodeJS

I'm trying to access data from a json but i can't... I have tried some explanations i found on the inte.net, but i guess there is something wrong on my code.

I'm getting data from a sql server database using the following code:

async function getLogin1(Operador, Senha) { 
    try {
        let pool = await sql.connect(config);
        let getLogin1 = await pool.request()
            .input('input_parameter', sql.VarChar, Operador)
            .input('input_parameter1', sql.VarChar, Senha)
            .query("SELECT Codigo, Operador, Senha FROM Usuario where Operador = @input_parameter and Senha = @input_parameter1");
        return getLogin1.recordsets;
    }
    catch (error) {
        console.log(error);

    }
}

So i get the recordsets here and put in a json:

router.route("/Login1").get((request, response) => {
    console.log(request.body.operador);
    console.log(request.body.senha);
    Operador = request.body.operador;
    Senha = request.body.senha;

    dboperations.getLogin1(Operador, Senha).then(result => {
        console.log(Operador, Senha);
        response.json(result);
        var json = JSON.stringify(result);
        console.log(json);
    })

})

On the console it shows the json:

[[{"Codigo":1,"Operador":"Username","Senha":"123456"}]]

I would like to get the individual data (codigo, operador and senha) to put in a individual string each one, but i cant access the data. When I try like json[0] for example i get all the json (because my json has every info on the first position i guess), and when i try json.Codigo (for example) i get a "undefined" error.

What am i doing wrong and what the best way to solve?

And sorry for the low knowledge, this is my very first api.

(And yes, this is a login code and its not the best way to treat user data but its a very small system for intern use)

Your JSON "result" is:

[[{"Codigo":1,"Operador":"MASTER","Senha":"TERA0205"}]]

So it is an array of array containing one object with keys Codigo, Operador, Senha.

To get the value of the Codigo of that object you would likely need to access it like

var CodigoVal = result[0][0].Codigo; // 1
var OperadorVal = result[0][0].Operador; // "MASTER"

Looks like json has nested array, if so json[0] will give inner array so you have to probably do like json[0][0].Codigo

[
 [
  { "Codigo":1,
    "Operador":"Username",
    "Senha":"123456"
  }
 ]
]

If we look at the JSON you posted above, we have an array which has an array with an object at its first index.

So to get access to that object you need to do:

const obj = json[0][0];

Now from obj you can extract Codigo, Operado and Senha like

const condigo = obg.Condigo;
const operado = obg.Operado;
const senha = obg.Senha;

You can also directly access them like

var codigo = json[0][0].Codigo;
var operador = json[0][0].Operador;
var senha = json[0][0].Senha;

json.Codigo doesnt work because your json has a array in it. Which has the object you are trying to get data from. json[0] returns everything because that gives you the object from that array

so if you want Codigo try json[0][0].Codigo

I hope this helped you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM