JSON.parse non-white spave error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JSON.parse non-white spave error

Hello Everyone, I've been trying to get some code to work by using the JSON.parse() function, however, it doesn't sem to want to work due to a whitespace issue. I'm trying to make a game taht will save a set of variables and then load them back up using the localStorage API. I've tried various ways to get this to work but so far i've bee out of luck. I treid to make stings like this var = string + string + string var += string var = array.push(string) If anyone can advise, that would be great. If you want to see the issue at work, heres the ink. https://www.sololearn.com/compiler-playground/WJRxRMvP9dm0

22nd Nov 2023, 12:57 AM
Callum Stewart
Callum Stewart - avatar
4 Answers
+ 2
Callum Stewart I'm having a hard time testing it since I'm on the app version. I am hacking your code to save and read from a global string variable savestring. Your auto save function seems to be working ok. So your savestring is a mashup of JSON object and list...and you're having a hard time reading that? Maybe organize them into a master JSON object so they can be more easily accessed , then stringify that? More sensible than just concatenatng them directly. or you can save them separately. or instead of string, make saveString an object and save that. function saveGame() { let saveString = { gameState, commonCard, reputationBonus, uncommonCard, boosterPack, productList } localStorage.setItem( 'gameSaveData', saveString) }
22nd Nov 2023, 7:55 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li the save game function is created at line 42, where the strings are created. The load game function starts at 62 where it attempts to access local storage The occur occurs at line 64 where it can't praise properly. LocalStoarage is available on the browser version of sololearn rather than the app version
22nd Nov 2023, 7:34 AM
Callum Stewart
Callum Stewart - avatar
+ 1
I used your last suggestion and got it working, a few more issues cropped up but those soon got fixed. Thank you Bob_Li
22nd Nov 2023, 1:52 PM
Callum Stewart
Callum Stewart - avatar
0
Callum Stewart I can't find the part of the code you're referring to. Also, you can't access local storage with Sololearn app. https://code.sololearn.com/WJRxRMvP9dm0/?ref=app
22nd Nov 2023, 4:31 AM
Bob_Li
Bob_Li - avatar