Page 1 of 1

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

Posted: Tue Oct 08, 2013 11:31 am
by Ram

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.


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

Posted: Tue Oct 08, 2013 11:53 am
by Maryna Brodina

Hello! Working on it.


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

Posted: Tue Oct 08, 2013 12:00 pm
by Ram

thx much!


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

Posted: Tue Oct 08, 2013 12:16 pm
by Maryna Brodina

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.


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

Posted: Tue Oct 08, 2013 12:18 pm
by Ram

Many many thanks!!


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

Posted: Tue Oct 08, 2013 12:22 pm
by Ram

working perfectly!!!
THANKS!!!


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

Posted: Tue Oct 08, 2013 12:23 pm
by Ram

working perfectly!!!