[英]JSON.Net - Reordering JSON when serializing
我從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?
我不知道為什么他們會按順序出現,並且實際上對他們沒有任何控制權。
只要所有問題都遵循number. question
模式number. question
number. question
然后以下將在反序列化之后對它們進行排序:
webinarQuestions.questions = webinarQuestions.questions
.OrderBy(q => int.Parse(q.question.Split('.')[0])).ToList();
它很hacky,但是處理的問題號大於9。
有什么原因不能使用Enumerable.OrderBy?
首先將JSON反序列化為List<WebinarQuestion>
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();
現在問題已經解決了。
干杯
編輯:如前所述,我的原始答案將無法解決9個以上的問題。 現在,就像其他人所做的那樣,並使用Split
,我們當然必須進行某種形式的錯誤檢查,以防格式錯誤。
為了簡潔起見,我決定將其省略。
使您的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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.