如何像这样从 MVC 5 控制器格式化 json?

How to format json from the MVC 5 Controller like this?

我有这个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}
        ]
    } 
]

我可以使用控制器生成它吗,还是我必须逐个获取数据并将其组合起来。我的问题的最佳解决方案是什么。提前感谢先生和女士

创建具有以下结构的视图模型:

public class Info
{
    public string name { get; set; }
    public string main { get; set; }
    public string results { get; set; }
}

public class A
{
    public string option { get; set; }
    public bool correct { get; set; }
}

public class Question
{
    public string q { get; set; }
    public List<A> a { get; set; }
}

public class RootObject
{
    public Info info { get; set; }
    public List<Question> questions { get; set; }
}

根据您的喜好重命名 类。 我用的是json2csharp代码生成工具吧。

您的控制器操作代码将变为:

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 Info
                           {
                               name = a.info.name,
                               main = a.info.main,
                               results = a.info.result
                           },
                           questions = new List<Question>() { new Question
                           {
                               q = b.Question.question1,
                               a = new List<A> () { new A
                                {
                                   option = b.Answer.option,
                                   correct = b.Answer.correct
                                }
                              }
                           }
                          }
                       };
        return Json(quizJSON, JsonRequestBehavior.AllowGet);
  }