62,052
社区成员
发帖
与我相关
我的任务
分享
//最后改了下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace ConsoleApplication1
{
[DataContract]
public sealed class FooDef
{
[DataMember(Name = "ID", IsRequired = true)]
public string ID { get; set; }
[DataMember(Name = "Order", IsRequired = true)]
public string Order { get; set; }
}
class Program
{
static void Main(string[] args)
{
string bar = "[{\"ID\":\"3\",\"Order\":\"9\"},{\"ID\":\"1\",\"Order\":\"6\"},{\"ID\":\"2\",\"Order\":\"8\"}]";
List<FooDef> newEntities = Deserialize<FooDef>(bar);
var s = from l in newEntities orderby l.ID select l;
}
static List<T> Deserialize<T>(string str)
{
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(List<T>));
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(str)))
{
List<T> entities = (List<T>)json.ReadObject(stream);
stream.Close();
return entities;
}
}
}
}
//
引用
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
//实现
[DataContract]
public sealed class FooDef
{
[DataMember(Name = "ID", IsRequired = true)]
public string ID { get; set; }
[DataMember(Name = "Order", IsRequired = true)]
public string Order { get; set; }
}
class Program
{
static void Main(string[] args)
{
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(List<FooDef>));
string bar = "[{\"ID\":\"3\",\"Order\":\"9\"},{\"ID\":\"1\",\"Order\":\"6\"},{\"ID\":\"2\",\"Order\":\"8\"}]";
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(bar));
List<FooDef> entities = (List<FooDef>)json.ReadObject(stream);
stream.Close();
//newEntities 就是排序好的
var newEntities = from l in entities orderby l.ID select l;
}
}