Phillip Navarrete
Phillip Navarrete

Reputation: 1

Sending a JSON to a server

So I have this code creating a game in p5.js and I was planning on sending it to a server or something so that people playing it can save data. I have tried using saveJSON() within p5.js but that only downloads the relevant data which isn't very useful in this case. I then tried using JS cookies;

document.cookie = "systems =" + json.systems + "; expires=Thu, 18 Dec 2020 12:00:00 UTC";

but they all just end up [object,Object] so again useless. Wanted to know if anyone had any solutions that would allow the user to save the game either client side or server side that wont require them to upload a file?

Upvotes: 0

Views: 108

Answers (1)

Krieger
Krieger

Reputation: 90

If you have a javascript object you can convert it to json via stringify

JSON.stringify(json.systems)

Then to convert a json string to a javascript object:

JSON.parse(jsonString)

This doesn't seem like you need to save it on the server (although I could be wrong), checkout web storage. Store the json in web storage

localStorage.setItem("jsonData", JSON.stringify(json.systems));

Then load it from storage

json = JSON.parse(localStorage.getItem("jsonData"));

Upvotes: 1

Related Questions