Read JSON Key with Javascript

Asked

Viewed 403 times

1

I’m having a hard time trying to read the value of a key specific to my JSON.

I have the following JSON that is stored in my LocalStorage:

{idCmos: "83903", codigoPlang: "302432", tipoPlang: "39ieej", nome: "TaskControl"}

I am storing in a variable, as below:

  var dados_usuario = localStorage.getItem('dados_usuario');

When I try to read the value of the key nome via alert(), is returning the message Undefined.

Go on down mine alert:

alert (dados_usuario.nomeEntidade);
  • 1

    But you want to read the key or the value for a key?

  • The value of the key

  • I corrected the question

  • 1

    In that case, you will have to post the JSON. Run a console.log(dados_usuario) and put the result in your question.

  • I’ve uploaded the console output.

  • 2

    Try the answer hint below and remember to fix "the key": dados_usuario.nomeEntidade for dados_usuario.nome. ;d

Show 1 more comment

2 answers

4


The localStorage cannot store objects, objects are references, and references are not shared between different contexts. Once you close the page, or open a tab, this reference will not be able to access the object as it does not exist in its context.

What you can do is turn Json into a string, and then decode it back, using JSON.stringify and JSON.parse. Example:

const dados = {idCmos: "83903", codigoPlang: "302432", tipoPlang: "39ieej", nome: "TaskControl"};
localStorage.setItem('dados', JSON.stringify(dados));

const dados = JSON.parse(localStorage.getItem('dados'));
console.log(dados.idCmos);
  • It worked, thank you very much!!

2

localStorage saves a string then to save you must do the following:

localStorage.setItem('dados_usuario', JSON.stringify(dados_usuario ));

And to read:

const dados_usuario = JSON.parse(localStorage.getItem('dados_usuario'));

You can test with the following code:

const gravar = { idCmos: "83903", codigoPlang: "302432", tipoPlang: "39ieej", nome: "TaskControl" };

// Grava a variável no localStorage
localStorage.setItem('dados_usuario', JSON.stringify(gravar));
// Lê do localStorage
const lido = JSON.parse(localStorage.getItem('dados_usuario'));

console.log(lido.nome);

Storage

The Web Storage API interface provides access to session storage or local storage for a specific domain, allowing you, for example, to add, modify or delete stored data items.


localStorage

The localStorage property allows you to access a local Storage object. A localStorageis similar to sessionStorage. The only difference is that while the data stored on localStoragedo not expire, the data in sessionStorage has its data cleared when the page session expires - that is, when the page (tab or window) is closed.


sessionStorage

The sessionStorage property allows you to access a Session Storage object. A sessionStorageis similar to localStorage, the only difference is that while the data stored in localStoragedo not expire, data in the sessionStorage You have your data cleared at the end of the page session. The page session lasts while the browser is open and stays in the page reload. Opening the page in a new tab or window will generate a new session, which differentiates how cookies work.


JSON.parse

The method JSON.parse() parses a JSON string, constructing the value or an object JavaScript described by string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.


JSON.stringify

The method JSON.stringify() converts values into JavaScript for a String JSON. These values can be replaced by specifying the function replacer, or including only specific properties, when the replacer is specified.

  • Friend, it worked. Thank you very much!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.