How to access a particular value from json.i

I have stored a json in local storage.
The stored json data is as follows -
{“0”:“Anirban Roy”,“1”:“8876019127”,“name”:“Anirban Roy”,“u_mobile”:“8876019127”}.

Now trying to get and display only name from this json. Below is my code. log displaying “name: undefined.”

 let data = localStorage.getItem('userData');
    console.log('name: ';

Try ionic data storage:

Not an issue with localstorage. I have stored value successfully. Just unable to get a particular value. In my case I need only value of name attribute from the entire json.

You should JSON.stringify when store the data, and JSON.parse when load.


Thank you very much. Now I am able to get the desired value. Below is my modified lines of code-

    let retrievedObject = localStorage.getItem('userData');
    let stored_data = JSON.parse(retrievedObject);
    console.log('retrievedObject: ',stored_data.u_mobile );
1 Like

I disagree, and urge you to revisit this decision. localStorage is never the right choice IMHO, because you have no guarantee when it will be wiped out from underneath you.

Additionally, if you did migrate to Ionic Storage, there would be no need whatsoever for the manual JSON stringification and parsing you are doing.

1 Like

Thanks for clarification. I will migrate from localstorage to ionic storage.