JSON.Net-序列化时重新排序JSON

本文关键字:JSON 排序 Net- 序列化 新排序 | 更新日期: 2023-09-27 18:26:26

我从GoToWebinar的API调用中得到了一些类似的JSON:

[
   {
      "answerswer":"The Answer for Question 1",
      "question":"1. This is the Question?"
   },
   {
      "answerswer":"The Answer for Question 2",
      "question":"2. This is the Question?"
   },
   {
      "answerswer":"The Answer for Question 7",
      "question":"7. This is the Question?"
   },
   {
      "answerswer":"The Answer for Question 5",
      "question":"5. This is the Question?"
   },
   {
      "answerswer":"The Answer for Question 3",
      "question":"3. This is the Question?"
   },
   {
      "answerswer":"The Answer for Question 8",
      "question":"8. This is the Question?"
   },
   {
      "answerswer":"The Answer for Question 4",
      "question":"4. This is the Question?"
   },
   {
      "answerswer":"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; }
}

我希望网络研讨会问题。问题按顺序排列。有没有一种方法可以在不迭代JSON的情况下做到这一点?

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

JSON.Net-序列化时重新排序JSON

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

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

它很有技巧,但它能处理大于9的问题。

这样做:

string jsonQuestions = @"[
{
  ""answerswer"":""The Answer for Question 1"",
  ""question"":""1. This is the Question?""
},
{
  ""answerswer"":""The Answer for Question 2"",
  ""question"":""2. This is the Question?""
},
{
  ""answerswer"":""The Answer for Question 7"",
  ""question"":""7. This is the Question?""
},
{
  ""answerswer"":""The Answer for Question 5"",
  ""question"":""5. This is the Question?""
},
{
  ""answerswer"":""The Answer for Question 3"",
  ""question"":""3. This is the Question?""
},
{
  ""answerswer"":""The Answer for Question 8"",
  ""question"":""8. This is the Question?""
},
{
  ""answerswer"":""The Answer for Question 4"",
  ""question"":""4. This is the Question?""
},
{
  ""answerswer"":""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所做的那样,我们当然必须进行某种形式的错误检查,以防格式错误。

为了简洁起见,我决定省略这一点。

您为什么不能使用Enumerable.OrderBy?

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

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

让您的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();

编辑:如果你想保留你的网络研讨会问题类别:

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