Dear Appery,
i have JSON data saved in a localStorage, the data looks like this:
can you pls point me on how to retrieve for example the data in field="male" and index="1" (U.S. English) using JS?
Thanks much in advance!
Best,
R.
Hello! Working on it.
It's not clear from your screenshot are those objects in array or object. Anyway they should be in array. So in localStorage should be the following line as elements order in array is not defined:
pre[{"language": "U.S. English", "female": "Allison, Susan", "male":"Dave, Steven, Victor"}, {"language": "U.S. English", "female": "Allison, Susan", "male":"Dave, Steven, Victor"}]/pre
Then you can parse it to variable and continue work with objects array:
pretry {
var arr = JSON.parse(localStorage.getItem("varName"));
if ({}.toString.call(arr) !== "[object Array]") {
alert("Some wierd data in localstorage...");
} else {
alert(arr[0]['male']);
}
} catch ( e ) {
alert("Parse Error. Some wierd data in localstorage...");
}/pre
where varName - variable name in localStorage
[0] - firat element (indexes counting from 0)
block try-catch and this part preif ({}.toString.call(arr) !== "[object Array]")/pre are added because in localStorage might be stored data you're not expecting. So you always have to check what you get in response.
Many many thanks!!
working perfectly!!!
THANKS!!!
working perfectly!!!