我可以在JavaScript数据库中存储JavaScript对象吗? [英] Can I store a JavaScript Object in a mySQL database?
问题描述
我正在从网页访问者那里收集数据,并将其放入我创建的JavaScript对象中。但是以后我希望能够引用他们输入的数据。
I am gathering data from a webpage visitor and putting it into a JavaScript object I create. But later I want to be able to reference the data they entered.
我可以访问MySQL数据库,那么有什么办法可以在其中存储该对象?
I have access to a MySQL database, so is there a way for me to store this object there?
我想尝试将其保留为对象格式,而不是将其分解为单独的部分。
I want to try and keep it in the object format instead of breaking it up into its separate parts.
推荐答案
在数据库中存储对象的 JSON.stringified
版本,然后 JSON.parse
当您想要再次返回对象时。看起来像这样:
Store a JSON.stringified
version of your object in the database, then JSON.parse
it when you want the object back again. It would look something like this:
var myObj = {some: data, other: stuff};
var myObjString = JSON.stringify(myObj);
// store string in mySQL database here
// load string from database
var myJSONString = //mySQL database call
var myLoadedObj = JSON.parse(myJSONString);
这篇关于我可以在JavaScript数据库中存储JavaScript对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!