Can I store a JavaScript Object in a mySQL database?

前端 未结 5 527
谎友^
谎友^ 2021-02-07 20:59

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.

I have acce

5条回答
  •  青春惊慌失措
    2021-02-07 21:19

    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);
    

提交回复
热议问题