Local storage save json

how to save my data json format in my local storage.
how to set the value.any idea guys??
tnx in advance. :slight_smile:

I think this will give you an idea. if you want to save an object like this:
var jsonObj = { name: ‘John Doe’,
sex:‘male’,
address:‘Lagos, Nigeria’};

window.localStorage.setItem(‘myJsonKey’, JSON.stringify(jsonObj));

And you can retrieve this like this:

var retrievedObj = JSON.parse(localStorage.getItem(‘myJsonKey’));

This can also be done if you have array of objects.

Lets say you have something like this

var employees= [
{
“id”: “1”,
“firstName”: “Tom”,
“lastName”: “Cruise”
},
{
“id”: “2”,
“firstName”: “Maria”,
“lastName”: “Sharapova”
},
{
“id”: “3”,
“firstName”: “James”,
“lastName”: “Bond”
}
]

Just do it the same way:

window.localStorage.setItem(‘myemps’, JSON.stringify(employees));

Hope this helps

5 Likes

IMHO ionic-storage is a much better option than the previous suggestion.

I would prefer using IonicStorage as well, here is an working example of its implementation: Ionic Storage: Ionic 2

how to set the value here for my database in 000webhost?

// set a key/value
storage.set(‘name’, ‘Max’);

 // Or to get a key/value pair
 storage.get('name').then((val) => {
   console.log('Your name is', val);
 })

}
}

this is my json database:

[{“act_id”:“1”,“act_title”:“Thanksgiving Day”,“act_date”:“November 27, 2016”,“act_content”:“To gather in unity ? It is refreshing and invigorating when people come together, in celebration of a common purpose. It is a reconciliation of differences as well as a time of healing. In sharing our victories as well as our struggles, we find strength and hope…”}]

how to set the value here for my database in 000webhost?.

// set a key/value
storage.set(‘name’, ‘Max’);

 // Or to get a key/value pair
 storage.get('name').then((val) => {
   console.log('Your name is', val);
 })

}
}

this is my json database:

[{“act_id”:“1”,“act_title”:“Thanksgiving Day”,“act_date”:“November 27, 2016”,“act_content”:“To gather in unity ? It is refreshing and invigorating when people come together, in celebration of a common purpose. It is a reconciliation of differences as well as a time of healing. In sharing our victories as well as our struggles, we find strength and hope…”}]

I’m thinking of use Ionic’s Local Storage API https://ionicframework.com/docs/v2/storage/ to build up an offline mode app, but I have a few question that still not clear to me:

  1. How would I structure my local database? I’ve seen that people are storing the whole database in one key like storage.set(mydatabase, [whole data stored in my app]), but how does it affect the performance if my database has 10MB or more? will the whole data be loaded in memory? if so It will slow down the app I guess

  2. How would I be querying the data?

  3. I know it is NoSql database but how do you track record relations?

Thanks in advice

Take a look at SQLite or Firebase, it could help you.

Thanks, I’ve developed the app using ionic storage as NoSQL and it worked like a charm.

if you have a complex database and you may update records you can use [Native SQLite ] (https://ionicframework.com/docs/native/sqlite/)

1 Like

I do not understand, what is the problem with localstorage? It works very fine for me. Or is there any security reason?

It provides no useful guarantee of persistence. It can get wiped at any time the OS feels like it.

Ah, thank you. Good to know :slight_smile: