序列化:将数据结构反序列化到/从JSON [英] serialize : deserialize data structure to/from JSON
问题描述
我有一个这样的测验程序的数据结构:
qData = {
qQuestionText:'',qAnswer1:''
,qAnswer2:'',qAnswer3:'',qAnswer4:'' qGoodAns:'',qAnswerText:''};
一个测验是由一定数量的这样的问题组成的,我想将每个问题的数据存储在内存中或者作为JSON对象发送到数据库。
我怀疑我将使用var questionList = new Array();并且每个项目将是一个JSON。
如何序列化/反序列化给定的问题?
如何初始化一个新的问题结构?
实现这一点的最佳做法是什么?
您可以使用浏览器中的内置JSON对象,除非您希望支持旧的IE版本
查看此链接:将数组转换为JSON 。
您不需要在序列化的JSON-String中发送单个问题,您还可以将整个问题数组或其一部分与数据库进行同步。
//创建问题对象
var q1 = {question:你的问题1 };
var q2 = {question:你的问题2};
//创建问题数组
var questionList = new Array();
//向列表中添加问题
questionList.push(q1);
questionList.push(q2);
var yourJsonString = JSON.stringify(questionList);
console.log(jsonstring of questions:+ yourJsonString);
var newList = JSON.parse(yourJsonString);
//循环通过解析的数组对象
for(var i in newList){
//使用tmp获取问题对象
var tmp = newList [i];
//做任何你想要的
console.log(tmp.question);
}
,你会得到一些这样的输出
jsonstring of questions:[{question:your question 1},{question:你的问题2}]
你的问题1
你的问题2
更多信息到内置的JSON对象可以找到在
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
在这个例子中,不需要JQuery.parseJSON()。当然可以使用JQuery,如果你想。有关JQuery.parseJSON()的更多信息可以在 https://api.jquery.com/jQuery.parseJSON / 。我没有看到在你的情况下使用JQuery.parseJSON()的必要性。
I'm fairly new to Javascrit / JQuery.
I have a data structure for a quiz program like this:
qData = {
qQuestionText: '', qAnswer1: ''
, qAnswer2: '', qAnswer3: '', qAnswer4: '', qGoodAns: '', qAnswerText: ''};
A quiz is made of a certain number of such questions and I want to store each question's data in memory or send to a database as a JSON object.
I suspect I will use var questionList = new Array(); and each item will be a JSON.
How do I serialize / deserialize a given question? How do I initialize a new question structure?
What is best practice to achieve this?
You can use the build-in JSON Object in Browser, unless you want to support the old IE version take a look at this link: Convert array to JSON.
You don't need to send a single question in serialized JSON-String, you can also synchronize the whole question array or a part of it with your data base. Try this in your case.
// create question objects
var q1 = {question:"your question 1"};
var q2 = {question:"your question 2"};
// creat question array
var questionList = new Array();
// add question to List
questionList.push(q1);
questionList.push(q2);
var yourJsonString = JSON.stringify(questionList);
console.log("jsonstring of questions: " + yourJsonString);
var newList = JSON.parse(yourJsonString);
// loop through the parsed Array Object
for (var i in newList) {
// use tmp to get the question object
var tmp = newList[i];
// do what ever you want
console.log(tmp.question);
}
and you will get some output like this
jsonstring of questions: [{"question":"your question 1"},{"question":"your question 2"}]
your question 1
your question 2
more info to the build-in JSON Object can be found under
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
In this example, the JQuery.parseJSON() are not needed. Surely you can use JQuery, if you want to. more about JQuery.parseJSON() can be found unter https://api.jquery.com/jQuery.parseJSON/ . I don't see the necessity to use JQuery.parseJSON() in your case.
这篇关于序列化:将数据结构反序列化到/从JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!