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

C# JSON.Net-序列化时重新排序JSON,c#,json,json.net,C#,Json,Json.net,通过调用GoTowBinar的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?" }, { "answ

通过调用GoTowBinar的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; }
}
公共课网络研讨会问题{
公共列表问题{get;set;}
}
公开课网上研讨会问题{
公共字符串答案{get;set;}
公共字符串问题{get;set;}
}
我想把网络研讨会的问题整理好。有没有一种方法可以在不迭代JSON的情况下做到这一点


我不知道为什么它们会按这种顺序出现,而且实际上对它们没有任何控制。

有什么原因不能使用Enumerable.OrderBy吗

首先将JSON反序列化为
List
Enumberable对象,然后使用
OrderBy

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

有什么原因不能使用Enumerable.OrderBy吗

首先将JSON反序列化为
List
Enumberable对象,然后使用
OrderBy

questions = questions.OrderBy(x => x.question);
这样做:

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();
string jsonQuestions=@”[
{
“答案”:“问题1的答案”,
“问题”:“1.这就是问题?”
},
{
“答案”:“问题2的答案”,
“问题”:“2.这就是问题?”
},
{
"答案:""问题7的答案",,
“问题”:“7.这就是问题?”
},
{
"答案:""问题5的答案",,
“问题”:“5.这就是问题?”
},
{
"答案:""问题3的答案",,
“问题”:“3.这就是问题?”
},
{
"答案:""问题8的答案",,
“问题”:“8.这就是问题?”
},
{
"答案:""问题4的答案",,
“问题”:“4.这就是问题?”
},
{
"答案:""问题6的答案",,
“问题”:“6.这就是问题?”
}
]";
WebinarQuestions wq=新的WebinarQuestions();
wq.questions=JsonConvert.DeserializeObject(jsonQuestions).OrderBy(x=>x.question.Split('.')[0]).ToList();
现在问题已经准备好了

干杯

编辑:如前所述,我最初的答案对9个以上的问题都不起作用。现在,正如其他人所做的那样,使用
Split
,我们当然必须进行某种形式的错误检查,以防格式错误

为了简洁起见,我决定省略此项。

请执行以下操作:

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();
string jsonQuestions=@”[
{
“答案”:“问题1的答案”,
“问题”:“1.这就是问题?”
},
{
“答案”:“问题2的答案”,
“问题”:“2.这就是问题?”
},
{
"答案:""问题7的答案",,
“问题”:“7.这就是问题?”
},
{
"答案:""问题5的答案",,
“问题”:“5.这就是问题?”
},
{
"答案:""问题3的答案",,
“问题”:“3.这就是问题?”
},
{
"答案:""问题8的答案",,
“问题”:“8.这就是问题?”
},
{
"答案:""问题4的答案",,
“问题”:“4.这就是问题?”
},
{
"答案:""问题6的答案",,
“问题”:“6.这就是问题?”
}
]";
WebinarQuestions wq=新的WebinarQuestions();
wq.questions=JsonConvert.DeserializeObject(jsonQuestions).OrderBy(x=>x.question.Split('.')[0]).ToList();
现在问题已经准备好了

干杯

编辑:如前所述,我最初的答案对9个以上的问题都不起作用。现在,正如其他人所做的那样,使用
Split
,我们当然必须进行某种形式的错误检查,以防格式错误


为了简洁起见,我决定不提这个问题。

只要问题都遵循
数字模式。问题
反序列化后,以下内容将对其进行排序:

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

这是一个黑客程序,但它可以处理大于9的问题。

只要问题都遵循
数字模式。问题
反序列化后,以下内容将对其进行排序:

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

这是一个黑客程序,但它可以处理大于9的问题编号。

使您的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]); 
        }
    }
}
公共类网络研讨会问题:IComparable{
公共字符串答案{get;set;}
公共字符串问题{get;set;}
公共int比较(网络研讨会问题其他)
{ 
返回QuestionNumber.CompareTo(其他.QuestionNumber);
}
私人整数问题编号
{ 
得到
{ 
//或者,如果格式不同,则编写更健壮的解析
返回Int32.Parse(question.Split('.')[0]);
}
}
}
然后反序列化以列出:

var questions = JsonConvert.DeserializeObject<List<WebinarQuestion>>(json).OrderBy(x => x).ToList();
var questions=JsonConvert.DeserializeObject(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);
   }
}
公共课网络研讨会问题
{
公共网络研讨会问题(IEnumerable问题)
{
Questions=Questions.OrderBy(x=>x.ToList();
}
公共IReadOnlyList问题{get;private set;}
来自json(字符串json)的公共静态网络研讨会问题
{
var questions=JsonConvert.DeserializeObject(json);
返回新的网络研讨会问题(问题);
}
}

使您的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]); 
        }
    }
}
公共类网络研讨会问题:IComparable{
公共字符串答案{get;set;}
公共字符串问题{get;set;}
公共int比较(网络研讨会问题其他)
{ 
返回QuestionNumber.CompareTo(其他.QuestionNumber);
}
私人整数问题编号
{ 
得到
{ 
//或者,如果格式不同,则编写更健壮的解析
返回Int32.Parse(question.Split('.')[0]);
}
}
}
然后反序列化以列出:

var questions = JsonConvert.DeserializeObject<List<WebinarQuestion>>(json).OrderBy(x => x).ToList();
var questions=JsonConvert.DeserializeObject(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);
   }
}
公共课网络研讨会问题
{
公共网络研讨会问题(IEnumerable问题)
{
问题=问题。或