JSON.Net - 在序列化

时间:2015-12-11 21:41:42

标签: c# json json.net

我通过调用GoToWebinar的API获得了类似的JSON:

[
   {
      "answer":"The Answer for Question 1",
      "question":"1. This is the Question?"
   },
   {
      "answer":"The Answer for Question 2",
      "question":"2. This is the Question?"
   },
   {
      "answer":"The Answer for Question 7",
      "question":"7. This is the Question?"
   },
   {
      "answer":"The Answer for Question 5",
      "question":"5. This is the Question?"
   },
   {
      "answer":"The Answer for Question 3",
      "question":"3. This is the Question?"
   },
   {
      "answer":"The Answer for Question 8",
      "question":"8. This is the Question?"
   },
   {
      "answer":"The Answer for Question 4",
      "question":"4. This is the Question?"
   },
   {
      "answer":"The Answer for Question 6",
      "question":"6. This is the Question?"
   }
]

它将使用JSON.Net序列化以填充这些类:

public class WebinarQuestions {
    public List<WebinarQuestion> questions { get; set; }
}

public class WebinarQuestion {
    public string answer { get; set; }
    public string question { get; set; }
}

我希望WebinarQuestions.questions有序。有没有办法在不重复JSON的情况下执行此操作?

我不知道为什么他们按照这个顺序过来,并且对它们没有任何控制权。

4 个答案:

答案 0 :(得分:2)

只要问题都遵循number. question的模式,那么以下内容将在反序列化后对它们进行排序:

webinarQuestions.questions = webinarQuestions.questions
    .OrderBy(q => int.Parse(q.question.Split('.')[0])).ToList();

它很hacky但它​​处理大于9的问题数。

答案 1 :(得分:1)

你有什么理由不能使用Enumerable.OrderBy?

首先将JSON反序列化为List<WebinarQuestion> Enumberable对象,然后使用OrderBy对其进行排序

questions = questions.OrderBy(x => x.question);

答案 2 :(得分:1)

这样做:

string jsonQuestions = @"[
{
  ""answer"":""The Answer for Question 1"",
  ""question"":""1. This is the Question?""
},
{
  ""answer"":""The Answer for Question 2"",
  ""question"":""2. This is the Question?""
},
{
  ""answer"":""The Answer for Question 7"",
  ""question"":""7. This is the Question?""
},
{
  ""answer"":""The Answer for Question 5"",
  ""question"":""5. This is the Question?""
},
{
  ""answer"":""The Answer for Question 3"",
  ""question"":""3. This is the Question?""
},
{
  ""answer"":""The Answer for Question 8"",
  ""question"":""8. This is the Question?""
},
{
  ""answer"":""The Answer for Question 4"",
  ""question"":""4. This is the Question?""
},
{
  ""answer"":""The Answer for Question 6"",
  ""question"":""6. This is the Question?""
}
]";

WebinarQuestions wq = new WebinarQuestions();
wq.questions = JsonConvert.DeserializeObject<List<WebinarQuestion>>(jsonQuestions).OrderBy(x => x.question.Split('.')[0]).ToList();

问题现在正在按顺序排列。

干杯

编辑:正如所指出的那样,我原来的答案不会超过9个问题。现在,正如其他人已经完成并使用Split一样,当然我们必须在格式错误的情况下进行某种形式的错误检查。

为了简洁起见,我决定将其删除。

答案 3 :(得分:1)

使您的WebinarQuestion类实现IComparable,这将处理多位数的问题编号:

public class WebinarQuestion : IComparable<WebinarQuestion> {
    public string answer { get; set; }
    public string question { get; set; }

    public int CompareTo(WebinarQuestion other)
    { 
        return QuestionNumber.CompareTo(other.QuestionNumber);
    }

    private int QuestionNumber 
    { 
        get 
        { 
            // Or write more robust parsing if format differs
            return Int32.Parse(question.Split('.')[0]); 
        }
    }
}

然后反序列化到列表:

var questions = JsonConvert.DeserializeObject<List<WebinarQuestion>>(json).OrderBy(x => x).ToList();

编辑:如果你想保留你的WebinarQuestions类:

public class WebinarQuestions 
{
   public WebinarQuestions(IEnumerable<WebinarQuestion> questions)
   {
       Questions = questions.OrderBy(x => x).ToList();
   }

   public IReadOnlyList<WebinarQuestion> Questions { get; private set; }

   public static WebinarQuestions FromJson(string json)
   {
       var questions = JsonConvert.DeserializeObject<List<WebinarQuestion>>(json);
       return new WebinarQuestions(questions);
   }
}