简体   繁体   English

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

[英]JSON.Net - Reordering JSON when serializing

I have some JSON similar to this coming over from a call to GoToWebinar's API: 我从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?"
   }
]

It will be serialized using JSON.Net to populate these classes: 它将使用JSON.Net序列化以填充以下类:

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

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

I'd like the WebinarQuestions.questions to be in order. 我希望WebinarQuestions.questions井井有条。 Is there a way to do this without iterating over the JSON? 有没有一种方法,而无需遍历JSON?

I don't know why they come over in that order and don't really have any control over them. 我不知道为什么他们会按顺序出现,并且实际上对他们没有任何控制权。

As long as the questions all follow a pattern of number. question 只要所有问题都遵循number. question模式number. question number. question then the following will sort them after deserialization: number. question然后以下将在反序列化之后对它们进行排序:

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

It's hacky but it handles question numbers greater than 9. 它很hacky,但是处理的问题号大于9。

Any reason you couldn't use Enumerable.OrderBy? 有什么原因不能使用Enumerable.OrderBy?

First deserialize the JSON into List<WebinarQuestion> Enumberable object then sort it using OrderBy 首先将JSON反序列化为List<WebinarQuestion> Enumberable对象,然后使用OrderBy对其进行排序

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

Do this: 做这个:

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

questions are now in order. 现在问题已经解决了。

Cheers 干杯

EDIT: As pointed out, my original answer would not have worked for more than 9 questions. 编辑:如前所述,我的原始答案将无法解决9个以上的问题。 Now, as others have done and using Split , of course we must do some form of error checking in case the format is wrong. 现在,就像其他人所做的那样,并使用Split ,我们当然必须进行某种形式的错误检查,以防格式错误。

I have decided to leave this out for brevity. 为了简洁起见,我决定将其省略。

Make your WebinarQuestion class implement IComparable, this will handle multi digit question numbers: 使您的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]); 
        }
    }
}

Then deserialize to list: 然后反序列化以列出:

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

Edit: if you want to keep your WebinarQuestions class: 编辑:如果要保留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);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM