日期:2014-05-17 浏览次数:20766 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string oldStr = "{root:[{DivId:\"div1\",DivWidth:200,DivHeight:167,leftPosition:50,topPosition:34,DivParentId:\"lefts\"},{DivId:\"div10\",DivWidth:200,DivHeight:135,leftPosition:50,topPosition:208,DivParentId:\"lefts\"},{DivId:\"div9\",DivWidth:200,DivHeight:245,leftPosition:50,topPosition:350,DivParentId:\"lefts\"},{DivId:\"div8\",DivWidth:600,DivHeight:711,leftPosition:260,topPosition:34,DivParentId:\"centers\"},{DivId:\"div6\",DivWidth:600,DivHeight:551,leftPosition:260,topPosition:752,DivParentId:\"centers\"},{DivId:\"div3\",DivWidth:600,DivHeight:135,leftPosition:260,topPosition:1310,DivParentId:\"centers\"},{DivId:\"div5\",DivWidth:600,DivHeight:197,leftPosition:260,topPosition:1452,DivParentId:\"centers\"},{DivId:\"div4\",DivWidth:200,DivHeight:143,leftPosition:870,topPosition:34,DivParentId:\"rights\"},{DivId:\"div2\",DivWidth:200,DivHeight:135,leftPosition:870,topPosition:184,DivParentId:\"rights\"},{DivId:\"div7\",DivWidth:200,DivHeight:593,leftPosition:870,topPosition:326,DivParentId:\"rights\"}]}"; string newStr = "{root:[{DivId:\"div6\",DivWidth:200,DivHeight:167,leftPosition:50,topPosition:34,DivParentId:\"lefts\"},{DivId:\"div9\",DivWidth:200,DivHeight:135,leftPosition:50,topPosition:208,DivParentId:\"lefts\"},{DivId:\"div10\",DivWidth:200,DivHeight:245,leftPosition:50,topPosition:350,DivParentId:\"lefts\"},{DivId:\"div8\",DivWidth:600,DivHeight:711,leftPosition:260,topPosition:34,DivParentId:\"centers\"},{DivId:\"div1\",DivWidth:600,DivHeight:551,leftPosition:260,topPosition:752,DivParentId:\"centers\"},{DivId:\"div3\",DivWidth:600,DivHeight:135,leftPosition:260,topPosition:1310,DivParentId:\"centers\"},{DivId:\"div5\",DivWidth:600,DivHeight:197,leftPosition:260,topPosition:1452,DivParentId:\"centers\"},{DivId:\"div4\",DivWidth:200,DivHeight:143,leftPosition:870,topPosition:34,DivParentId:\"rights\"},{DivId:\"div2\",DivWidth:200,DivHeight:135,leftPosition:870,topPosition:184,DivParentId:\"rights\"},{DivId:\"div7\",DivWidth:200,DivHeight:593,leftPosition:870,topPosition:326,DivParentId:\"rights\"}]}"; iMove(Regex.Match(oldStr, @"{root:\[(.+)\]}").Groups[1].Value, Regex.Match(newStr, @"{root:\[(.+)\]}").Groups[1].Value); } static void iMove(string oldStr, string newStr) { var data1 = oldStr.Split(new string[] { "},{" }, StringSplitOptions.None).Select(x => new { id = Regex.Match(x, @"(?<=DivId:"")\w+?(?="")").Value, value = Regex.Match(x, @"(?<=DivParentId:"")\w+?(?="")").Value }).GroupBy(x => x.value).SelectMany(x => x.Select((y, i) => new { y.id, y.value, order = i + 1 })); var data2 = newStr.Split(new string[] { "},{" }, StringSplitOptions.None).Select(x => new { id = Regex.Match(x, @"(?<=DivId:"")\w+?(?="")").Value, value = Regex.Match(x, @"(?<=DivParentId:"")\w+?(?="")").Value }).GroupBy(x => x.value).SelectMany(x => x.Select((y, i) => new { y.id, y.value, order = i + 1 }));