2
我從谷歌Search Appliance的得到一個響應返回一個JSON對象提出的服務以JSON的形式按以下格式排序使用LINQ
string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}";
我想按名稱搜索結果列表alphabeticaly排序和變化判決案件的名字。 我可以在jQuery中做到這一點,但出於性能原因,寧願在服務器端做到這一點。
我可以排序結果,但返回IEnumarable<Result>
,但我似乎無法排序正在序列化的對象內的結果。
string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}";
JObject json = JObject.Parse(jsonString);
var gsaSuggestions = JsonConvert.DeserializeObject<GSASuggestion>(jsonString);
var orded = gsaSuggestions.ResultList.OrderBy<Result, string>(r => r.Name);
string output = JsonConvert.SerializeObject(gsaSuggestions);
}
[JsonObject(MemberSerialization.OptOut)]
public class GSASuggestion
{
[JsonProperty(PropertyName = "query")]
public string Query {get; set;}
[JsonProperty(PropertyName = "results")]
public List<Result> ResultList {get; set;}
}
public class Result
{
[JsonProperty(PropertyName = "name")]
public string Name {get; set;}
[JsonProperty(PropertyName = "type")]
public string Type {get; set;}
}
的結果應該是:
{ "query": "t", "results": [ { "name": "Tim", "type": "suggest" }, { "name": "Tom", "type": "suggest" }]};