Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Restoring object with JSON

    Hello. Am I using JSON correctly? I thought I understood it, but I'm trying to restore an object using JSON.parse and it's saying it's of type string after the parse, when what I want is an object.

    Code:
    save: function() {
        	localStorage.cannonballStats2 = JSON.stringify(this);
        },
    
    
      toJSON: function() {
        	var toSave = {
        		allTimeGoals: 0,
        		allTimeMisses: 0,
        		allTimeLongestChainMiss: 0,
        		allTimelongestChainGoal: 0,
        		allTimeGoalsCounter: 0
        	};
        	
        	return JSON.stringify(toSave);
        },
    
     load: function() {
            if (!localStorage.cannonballStats2) {
        		console.log("no cannonballStats2");
        		return;
        	}
        	
        	var savedStats = JSON.parse(localStorage.cannonballStats2);
          
        	console.log(typeof savedStats); // prints out string
        },

  • #2
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No need to reply. I've figured it out. In case any one stumbles across this; there is no need to stringify what you return from toJSON.

    I did try this early but some other bug in my code stopped the update changes from happening so that's why I was stuck and came here.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •