我收到了来自 Google Search Appliance 建议服务的响应,格式为 JSON 格式,格式如下
string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}";
我想按名称按字母顺序对结果列表进行排序,并将名称更改为句子大小写。我可以在 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" }]};