How to save a JSON locally?

1

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 to a JSON file locally. After saving, I move to the application written in C language that will open and interpret this JSON so that the data is saved in a database (SQLite3).

    
asked by anonymous 27.10.2015 / 19:46

1 answer

3

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 in a web service. In C #, for example, you can serialize an object and use 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 .

    
27.10.2015 / 19:50