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.
我正在从网页访问者收集数据并将其放入我创建的JavaScript对象中。但后来我希望能够引用他们输入的数据。
I have access to a MySQL database, so is there a way for me to store this object there?
我可以访问MySQL数据库,所以有没有办法在那里存储这个对象?
I want to try and keep it in the object format instead of breaking it up into its separate parts.
我想尝试将其保留为对象格式,而不是将其分解为单独的部分。
4 个解决方案
#1
11
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:
将对象的JSON.stringified版本存储在数据库中,然后在需要再次返回对象时将其JSON.parse存储。它看起来像这样:
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);
#2
9
No, you can't, at least not as it is.
不,你不能,至少不是这样。
You might be able serialise it to a string. If it consists entirely of arrays, simple objects, and the other data types supported by the JSON format (which it probably will do if it is user supplied data (the main exception being if binary files are uploaded)), then you could use a JSON serializer to do so. Modern browsers provide the JSON object for this purpose and json2.js will polyfill for older browsers. You could then store the string in the database.
您可以将其序列化为字符串。如果它完全由数组,简单对象和JSON格式支持的其他数据类型组成(如果它是用户提供的数据(如果上传二进制文件则主要例外,它可能会这样做)),那么你可以使用JSON序列化程序这样做。现代浏览器为此提供了JSON对象,json2.js将为旧浏览器进行polyfill。然后,您可以将该字符串存储在数据库中。
Not breaking it into separate parts does throw away the many of the advantages of using a relational database though (i.e. the relationships and the ability to perform useful searches).
不将它分成单独的部分确实会丢弃使用关系数据库的许多优点(即关系和执行有用搜索的能力)。
#3
#4
0
Just store it as a JSON object in a field using the varchar or text data type.
只需使用varchar或text数据类型将其作为JSON对象存储在字段中。
#1
11
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:
将对象的JSON.stringified版本存储在数据库中,然后在需要再次返回对象时将其JSON.parse存储。它看起来像这样:
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);
#2
9
No, you can't, at least not as it is.
不,你不能,至少不是这样。
You might be able serialise it to a string. If it consists entirely of arrays, simple objects, and the other data types supported by the JSON format (which it probably will do if it is user supplied data (the main exception being if binary files are uploaded)), then you could use a JSON serializer to do so. Modern browsers provide the JSON object for this purpose and json2.js will polyfill for older browsers. You could then store the string in the database.
您可以将其序列化为字符串。如果它完全由数组,简单对象和JSON格式支持的其他数据类型组成(如果它是用户提供的数据(如果上传二进制文件则主要例外,它可能会这样做)),那么你可以使用JSON序列化程序这样做。现代浏览器为此提供了JSON对象,json2.js将为旧浏览器进行polyfill。然后,您可以将该字符串存储在数据库中。
Not breaking it into separate parts does throw away the many of the advantages of using a relational database though (i.e. the relationships and the ability to perform useful searches).
不将它分成单独的部分确实会丢弃使用关系数据库的许多优点(即关系和执行有用搜索的能力)。
#3
0
Take a look at JSON. You can use something like jQuery to serialize form data and send it off to the server. Also you could just use plain old forms, and process input on the server side.
看看JSON。您可以使用类似jQuery的内容来序列化表单数据并将其发送到服务器。您也可以使用普通的旧表单,并在服务器端处理输入。
#4
0
Just store it as a JSON object in a field using the varchar or text data type.
只需使用varchar或text数据类型将其作为JSON对象存储在字段中。