如何使用javascript或json从数据集中获取数据 [英] How to fetch data from dataset using javascript or json
问题描述
大家好,
我有一个数据库,其中有一个名为question的表,另一个名为question_answers的表
有两种类型的问题:多选和单一回复..
我想用JSON来获取问题及其答案..
我正在使用来自SQL数据库的WebMethod检索数据
我的代码是:JSON
Hello everyone,
I have one database in which there is a table called question and another table called question_answers
there are 2 types of questions namely : multiple choice and single response..
I want to fetch the question and its answers using JSON ..
I am retrieving data using WebMethod from SQL database
my code is : JSON
function take_test(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "LoadTest.aspx/GetQuiz",
data: "{}",
dataType: "json",
success: function (data) {
// for (var i = 0; i < data.d.length; i++) {
// $("#tbDetails").append("<tr><td>" + data.d[i].UserId + "</td><td>" + data.d[i].UserName + "</td><td>" + data.d[i].Location + "</td></tr>");
// }
alert(data);
},
error: function (result) {
alert(result);
}
});
}
代码落后:
code behind :
[WebMethod]
// Let .net handle the request and response as json automatically.
public static FetchQuiz[] GetQuiz(FetchQuiz q)
{
DataSet ds = new DataSet();
List<FetchQuiz> details = new List<FetchQuiz>();
SQLDataAccessHelper sq = new SQLDataAccessHelper();
string query = "";
query = "select * from tbl_question_master order by Id";
ds = sq.ExecuteCommandText(query);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
FetchQuiz quiz = new FetchQuiz();
quiz.Question = ds.Tables [0].Rows[i]["question"].ToString();
quiz.Answer = ds.Tables[0].Rows[i]["answer"].ToString();
// quiz.CorrectAnswer = ds.Tables[0].Rows[i]["UserId"].ToString();
details.Add(quiz);
}
return details.ToArray();
}
}
public class FetchQuiz
{
public string Question { get; set; }
public string Answer { get; set; }
// public string CorrectAnswer { get; set; }
//public string Type { get; set; }
}
现在问题是我无法获取数据......我也是不知道如何迭代数据..
我的数据将采用这种格式
问题ID输入问题答案
1 MCQ abc a
1 MCQ abc b
1 MCQ abc c
2 SR xyz a
2 SR xyz b
i希望以这种格式显示div标签中的数据
[问题]
A [Option1]
B [Option2]
C [Option3]
等......
请帮我实现这个目标。
提前致谢
Krunal
Now the problem here is i m not able to get data... also i am not aware how to iterate with data..
my data would be in this format
Question Id Type Question Answer
1 MCQ abc a
1 MCQ abc b
1 MCQ abc c
2 SR xyz a
2 SR xyz b
i want to display data in div tag in this format
[Question]
A [Option1]
B [Option2]
C [Option3]
and so on...
Please help me to achieve this.
Thanks in advance
Krunal
推荐答案
.ajax({
类型:POST,
contentType:application / json; charset = utf-8,
u rl:LoadTest.aspx / GetQuiz,
data:{},
dataType:json,
success:function(data){
// for( var i = 0;我< data.d.length; i ++){
//
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "LoadTest.aspx/GetQuiz", data: "{}", dataType: "json", success: function (data) { // for (var i = 0; i < data.d.length; i++) { //
(#tbDetails)。append(< tr>< td>+ data.d [i] .UserId + < / td>< td>+ data.d [i] .UserName +< / td>< td>+ data.d [i] .Location +< / td>< / TR>中);
//}
alert(data);
},
错误:函数(结果){
alert(result);
}
});
}
("#tbDetails").append("<tr><td>" + data.d[i].UserId + "</td><td>" + data.d[i].UserName + "</td><td>" + data.d[i].Location + "</td></tr>"); // } alert(data); }, error: function (result) { alert(result); } }); }
代码落后:
code behind :
[WebMethod]
// Let .net handle the request and response as json automatically.
public static FetchQuiz[] GetQuiz(FetchQuiz q)
{
DataSet ds = new DataSet();
List<FetchQuiz> details = new List<FetchQuiz>();
SQLDataAccessHelper sq = new SQLDataAccessHelper();
string query = "";
query = "select * from tbl_question_master order by Id";
ds = sq.ExecuteCommandText(query);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
FetchQuiz quiz = new FetchQuiz();
quiz.Question = ds.Tables [0].Rows[i]["question"].ToString();
quiz.Answer = ds.Tables[0].Rows[i]["answer"].ToString();
// quiz.CorrectAnswer = ds.Tables[0].Rows[i]["UserId"].ToString();
details.Add(quiz);
}
return details.ToArray();
}
}
public class FetchQuiz
{
public string Question { get; set; }
public string Answer { get; set; }
// public string CorrectAnswer { get; set; }
//public string Type { get; set; }
}
现在问题是我无法获取数据......我也是不知道如何迭代数据..
我的数据将采用这种格式
问题ID输入问题答案
1 MCQ abc a
1 MCQ abc b
1 MCQ abc c
2 SR xyz a
2 SR xyz b
i希望以这种格式显示div标签中的数据
[问题]
A [Option1]
B [Option2]
C [Option3]
等......
请帮我实现这个目标。
提前致谢
Krunal
Now the problem here is i m not able to get data... also i am not aware how to iterate with data..
my data would be in this format
Question Id Type Question Answer
1 MCQ abc a
1 MCQ abc b
1 MCQ abc c
2 SR xyz a
2 SR xyz b
i want to display data in div tag in this format
[Question]
A [Option1]
B [Option2]
C [Option3]
and so on...
Please help me to achieve this.
Thanks in advance
Krunal
请看下面的链接。基本上它是一个名为knockout的javascript插件。
它将数据绑定到html控件,所以在你的问题中你可以例如do
Look at the link below. Basically it's a javascript plugin called knockout.
it binds data to html controls, so in your problem you can e.g. do
<div databind="foreach: { data: question, as 'question' }"></div>
然后是一些html来呈现每个问题,例如as
then some html to render each question e.g. as
<ul><li>Question 1: blah blah</li></ul>
如果您有一点时间学习它,那么将json数据处理为html是最好的选择。 />
来自json的淘汰赛 [ ^ ]
这篇关于如何使用javascript或json从数据集中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!