Ram
Posts: 0
Joined: Fri Aug 02, 2013 12:15 pm

how to get a specific index/field from JSON/local storage?

Dear Appery,

i have JSON data saved in a localStorage, the data looks like this:

Image

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.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

how to get a specific index/field from JSON/local storage?

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.

Ram
Posts: 0
Joined: Fri Aug 02, 2013 12:15 pm

how to get a specific index/field from JSON/local storage?

Many many thanks!!

Ram
Posts: 0
Joined: Fri Aug 02, 2013 12:15 pm

how to get a specific index/field from JSON/local storage?

working perfectly!!!
THANKS!!!

Ram
Posts: 0
Joined: Fri Aug 02, 2013 12:15 pm

how to get a specific index/field from JSON/local storage?

working perfectly!!!

Return to “Issues”