我收到一个来自Google搜索设备suggest服务的回复,格式为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" }]};