如何使用Web API中的FromData在Dictionary中接收JSON数据?

时间:2017-07-12 09:30:42

标签: c# asp.net-mvc list dictionary asp.net-web-api

以下是JSON示例,客户端将发送到我的API,名为' GetQuestion'

 {
                 "lstQuestions": [{
                    "QuestionCategory": 1,
                    "QuestionText": "what is m in mvc",
                    "OptionA": "model",
                     "OptionB": "view", 
                     "OptionC": "controller", 
                     "OptionD": "razor",
                    "CorrectOption": "A"
                },
                {
                    "QuestionCategory": 2,
                    "QuestionText": "How are you",
                    "OptionA": "fine",
                     "OptionB": "not fine", 
                     "OptionC": "ok", 
                     "OptionD": "not ok",
                    "CorrectOption": "A"
                }],
               "Status" : 1

          }

以下是我的控制器API代码:

 public class QuestionDetails
    {
        public List<Questions> lstQuestions { get; set; }
        public int Status { get; set; }
    }
    public class Questions
    {
        public string QuestionCategory { get; set; }
        public string QuestionText { get; set; }
        public string OptionA { get; set; }
        public string OptionB { get; set; }
        public string OptionC { get; set; }
        public string OptionD { get; set; }
        public string CorrectOption { get; set; }

    }

    [Route("GetQuestions")]
    [HttpPost]
    public HttpResponseMessage SendQuestionDetails([FromBody] QuestionDetails UserDetailInput)
    {

        HttpResponseMessage mesage = Request.CreateResponse(HttpStatusCode.OK, "Demo"); ;
        if (ModelState.IsValid)
        {
            //in progress
        }
        return mesage;

    }

我想要做的是如何使用Dictionary创建一个类并传递为参数,我不想使用List,因为它的重量和字典比List要快得多。

例如:

  public class QuestionDetails
    {
        public Dictionary<string, Questions> lstQuestions { get; set; }
        public int Status { get; set; }
    } 
public HttpResponseMessage SendQuestionDetails([FromBody] Dictionary<string, QuestionDetails> UserDetailInput)
    {

        HttpResponseMessage mesage = Request.CreateResponse(HttpStatusCode.OK, "Demo"); ;
        if (ModelState.IsValid)
        {
            //in progress
        }
        return mesage;

    }

2 个答案:

答案 0 :(得分:0)

不知道你在用Dictionary做什么比List要快得多,但你只需要发送JSON为

{
    "A": {
        "lstQuestions": {
            "A": {
                "QuestionCategory": 1,
                "QuestionText": "what is m in mvc",
                "OptionA": "model",
                "OptionB": "view",
                "OptionC": "controller",
                "OptionD": "razor",
                "CorrectOption": "A"
            },
            "V": {
                "QuestionCategory": 2,
                "QuestionText": "How are you",
                "OptionA": "fine",
                "OptionB": "not fine",
                "OptionC": "ok",
                "OptionD": "not ok",
                "CorrectOption": "A"
            }
        },
        "Status": 1
    }
}

答案 1 :(得分:-1)

希望下面的代码会清除您的查询。

   public HttpResponseMessage SendQuestionDetails([FromBody] Dictionary<string, QuestionDetails> UserDetailInput)
        {
   List<Questions> list = new List<Questions> { };
            list.Add(new Questions
            {
                CorrectOption = "CorrectOption1",
                OptionA = "OptionA1",
                OptionB = "OptionB1",
                OptionC = "OptionC1",
                OptionD = "OptionD1",
                QuestionCategory = "QuestionCategory1",
                QuestionText = "QuestionText1"
            });
            list.Add(new Questions
            {
                CorrectOption = "CorrectOption2",
                OptionA = "OptionA2",
                OptionB = "OptionB2",
                OptionC = "OptionC2",
                OptionD = "OptionD2",
                QuestionCategory = "QuestionCategory2",
                QuestionText = "QuestionText2"
            });

            Dictionary<QuestionDetails, int> dictionary = new Dictionary<QuestionDetails, int> { };
            QuestionDetails detail = new QuestionDetails { lstQuestions = list, Status = 1 };

            HttpResponseMessage mesage = Request.CreateResponse(HttpStatusCode.OK, detail);
            return mesage;
    return mesage;
    }