【发布时间】:2015-01-29 16:28:52
【问题描述】:
我有这种 JSON 格式
[
{
info: {
name: "Quiz no 1",
main: "this is description of quiz no 1",
results: "this is the result or remarks after the quiz."
},
question: {
q: "1 + 1 is?",
a: {
option: "one",
correct: false
}
}
},
{
info: {
name: "Quiz no 1",
main: "this is description of quiz no 1",
results: "this is the result or remarks after the quiz."
},
question: {
q: "1 + 1 is?",
a: {
option: "two",
correct: true
}
}
},
{
info: {
name: "Quiz no 1",
main: "this is description of quiz no 1",
results: "this is the result or remarks after the quiz."
},
question: {
q: "1 + 1 is?",
a: {
option: "three",
correct: false
}
}
},
{
info: {
name: "Quiz no 1",
main: "this is description of quiz no 1",
results: "this is the result or remarks after the quiz."
},
question: {
q: "1 + 1 is?",
a: {
option: "four",
correct: false
}
}
}
]
这是来自我的控制器的 JSONRESULT 这是我使用的代码
public JsonResult GetData()
{
var quizJSON = from a in db.infoQuestions
join b in db.QuestionAnswers1
on a.questionAnswerID equals b.questionAnswerID
select new
{
info = new
{
name = a.info.name,
main = a.info.main,
results = a.info.result
},
question = new
{
q = b.Question.question1,
a = new
{
option = b.Answer.option,
correct = b.Answer.correct
}
}
};
return Json(quizJSON, JsonRequestBehavior.AllowGet);
}
问题是我需要的 json 格式是这样的
"info": {
"name": "Test Your Knowledge!!",
"main": "<p>Think you're smart enough to be on Jeopardy? Find out with this super crazy knowledge quiz!</p>",
"results": "<h5>Learn More</h5><p>Etiam scelerisque, nunc ac egestas consequat, odio nibh euismod nulla, eget auctor orci nibh vel nisi. Aliquam erat volutpat. Mauris vel neque sit amet nunc gravida congue sed sit amet purus.</p>"
},
"questions": [
{
"q": "What number is the letter A in the English alphabet?",
"a": [
{"option": "8", "correct": false},
{"option": "14", "correct": false},
{"option": "1", "correct": true},
{"option": "23", "correct": false}
]
},
{
"q": "Which of the following best represents your preferred breakfast?",
"a": [
{"option": "Bacon and eggs", "correct": false},
{"option": "Fruit, oatmeal, and yogurt", "correct": true},
{"option": "Leftover pizza", "correct": false},
{"option": "Eggs, fruit, toast, and milk", "correct": true}
]
},
{
"q": "Where are you right now? Select ALL that apply.",
"a": [
{"option": "Planet Earth", "correct": true},
{"option": "Pluto", "correct": false},
{"option": "At a computing device", "correct": true},
{"option": "The Milky Way", "correct": true}
]
},
{
"q": "How many inches of rain does Michigan get on average per year?",
"a": [
{"option": "149", "correct": false},
{"option": "32", "correct": true},
{"option": "3", "correct": false},
{"option": "1291", "correct": false}
]
},
{
"q": "Is Earth bigger than a basketball?",
"a": [
{"option": "Yes", "correct": true},
{"option": "No", "correct": false}
]
}
]
我可以使用控制器生成它还是我必须逐块获取数据并将其组合起来。什么是我的问题的最佳解决方案。提前感谢先生和女士
【问题讨论】:
-
使用 ViewModel 类
-
谢谢@EhsanSajjad,但如何?
标签: asp.net-mvc json linq asp.net-mvc-5 jsonresult