How to save a JSON locally?

Asked

Viewed 7,608 times

2

Direct question: What is the best way to save a JSON file locally and how to do it?

Details: I have a web application that receives user data using the Facebook API (Javascript SDK), I need to save this data in a JSON file locally. After saved, step to the application written in C link that will open and interpret this JSON so that the data is saved in a database (Sqlite3).

  • Can be in the browser cache?

  • 2

    No, I need it on the machine anyway.

1 answer

4


If you are looking for a client-side solution only, one option is Localstorage.

var objteste = { 'um': 1, 'dois': 2, 'tres': 3 };

// Armazena no LocalStorage
localStorage.setItem('objteste', JSON.stringify(objteste));

// Obtém do LocalStorage
var objSalvo = localStorage.getItem('objteste');

console.log('objSalvo: ', JSON.parse(objSalvo));

However you mention database - which implies a web service. In C#, for example, you can serialize an object and use the Filesystem as a cache before sending it to the database:

var _data = new 
{
    um = 1,
    dois = 2,
    tres = 3
};

string json = JsonConvert.SerializeObject(_data);

System.IO.File.WriteAllText (@"C:\objeto.json", json);

Sources: 1, 2.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.