Ask coding questions

← Back to all posts
Parse JSON string to object array
mwilki7 (1133)

I'm attempting to turn an existing array into a json string so that this string can be saved for loading and saving levels in my game.

But when I parse the json string back into my array I get
entlist[i].draw is not a function
which leads me to believe the json to object didn't work properly.

Does anyone know how to do object -> json -> object conversions properly?
I want to be able to able to call the same member functions these objects had before they were converted.

function save()
    var jsonstring = JSON.stringify(entlist);
    $("#leveldata_textarea").val(''); // dont want any garbage that may have made its way in here
function load()
    var jsonstring = $("#leveldata_textarea").val();
    entlist = JSON.parse(jsonstring);

This image is a before-and-after snapshot of my array before json conversion and after.

Answered by malvoliothegood (871) [earned 5 cycles]
View Answer
malvoliothegood (871)

You cannot stringify object methods. Have a look at this repl. It may contain code you can use:

mwilki7 (1133)

@malvoliothegood Shame. Though it would probably be a lot more text than would be necessary. I decided to use a text area to store the necessary elements along with the classname as the header.

Thanks for answering.