c# – 将System.Drawing.Point转换为JSON.如何将’X’和’Y’转
发布时间:2020-12-15 17:23:45 所属栏目:百科 来源:网络整理
导读:我试图遵循 JavaScript和C#中的命名约定.当来回传递JSONized数据时,这会导致一些有趣的问题.当我访问x / y坐标客户端时,我希望该属性为小写,但服务器端为大写. 注意: public class ComponentDiagramPolygon{ public ListSystem.Drawing.Point Vertices { ge
我试图遵循
JavaScript和C#中的命名约定.当来回传递JSONized数据时,这会导致一些有趣的问题.当我访问x / y坐标客户端时,我希望该属性为小写,但服务器端为大写.
注意: public class ComponentDiagramPolygon { public List<System.Drawing.Point> Vertices { get; set; } public ComponentDiagramPolygon() { Vertices = new List<System.Drawing.Point>(); } } public JsonResult VerticesToJsonPolygon(int componentID) { PlanViewComponent planViewComponent = PlanViewServices.GetComponentsForPlanView(componentID,SessionManager.Default.User.UserName,"image/png"); ComponentDiagram componentDiagram = new ComponentDiagram(); componentDiagram.LoadComponent(planViewComponent,Guid.NewGuid()); List<ComponentDiagramPolygon> polygons = new List<ComponentDiagramPolygon>(); if (componentDiagram.ComponentVertices.Any()) { ComponentDiagramPolygon polygon = new ComponentDiagramPolygon(); componentDiagram.ComponentVertices.ForEach(vertice => polygon.Vertices.Add(vertice)); polygons.Add(polygon); } return Json(polygons,JsonRequestBehavior.AllowGet); } 我理解,如果我能够使用C#属性’JsonProperty’来自定义命名约定.然而,据我所知,这仅适用于我所拥有的课程. 如何在传回客户端时更改System.Drawing.Point的属性? 解决方法
您可以通过投射到新的匿名类型来作弊:
var projected = polygons.Select(p => new { Vertices = p.Vertices.Select(v => new { x = v.X,y = v.Y }) }); return Json(projected,JsonRequestBehavior.AllowGet); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |