【问题标题】:How to format json from the MVC 5 Controller like this?如何像这样从 MVC 5 控制器格式化 json?
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 感谢您的快速响应,但 jsonresult 仍然不是我需要的格式
  • [ { info: { name: "Quiz no 1", main: "this is description of quiz no 1", results: "this is the result or remarks after the quiz." }, questions: [ { q: "1 + 1 is?", a: [ { option: "one", correct: false } ] } ] }]
  • 可以列出信息下的所有问题和问题下的所有答案,就像我给出的示例中的那样
  • @eagoregordy 给我几分钟,我会检查的。如果没有实体框架,我只会虚设一些数据。
  • @eagoregordy 问题和信息在 json 头脑中处于同一水平,而不是在给出的示例中。
猜你喜欢
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多