I exported all the models from a Backbone collection with JSON.stringify(collection.toJSON()) and managed to save it in a text file.
I want to import it, to recreate the collection.
The thing is, if I copy the text content and paste in the console…
collection.add(<-- and paste the text here -->)
the collection is created and everything work fine, but if I get the file content (using the File API), it doesn’t work.
After a long inspection I think that the problem is here:
[{ ....... }] works but "[{ ..... }]" don’t work, I don’t understand what the difference between them is. How can convert from"[{ ..... }]" to [{ ....... }]?
Your data seems to have somehow become double-encoded. At some point after you already have a JSON string, you run
JSON.stringify()on it a second time. That’s why you have extra quotes on the outside.If you need to do it this way, you should try to call
JSON.parse()on the file contents before passing them tocollection.add().