微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 将System.Drawing.Point转换为JSON.如何将’X’和’Y’转换为’x’和’y’?

我试图遵循 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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐