Asp.net mvc 4 JSON.NET反序列化-转换值时出错

Asp.net mvc 4 JSON.NET反序列化-转换值时出错,asp.net-mvc-4,fabricjs,json-deserialization,Asp.net Mvc 4,Fabricjs,Json Deserialization,我有一个JSON对象(FabricJS),我想在POST请求中用JSON.NET反序列化它: "{\"objects\":[{\"type\":\"OpenLayout\",\"originX\":\"left\",\"originY\":\"top\",\"left\":300,\"top\":203,\"width\":200,\"height\":100,\"fill\":\"#f05a30\",\"stroke\":null,\"strokeWidth\":1,\"strokeDashA

我有一个JSON对象(FabricJS),我想在POST请求中用JSON.NET反序列化它:

"{\"objects\":[{\"type\":\"OpenLayout\",\"originX\":\"left\",\"originY\":\"top\",\"left\":300,\"top\":203,\"width\":200,\"height\":100,\"fill\":\"#f05a30\",\"stroke\":null,\"strokeWidth\":1,\"strokeDashArray\":null,\"strokeLineCap\":\"butt\",\"strokeLineJoin\":\"miter\",\"strokeMiterLimit\":10,\"scaleX\":1,\"scaleY\":1,\"angle\":0,\"flipX\":false,\"flipY\":false,\"opacity\":1,\"shadow\":null,\"visible\":true,\"clipTo\":null,\"backgroundColor\":\"\",\"rx\":0,\"ry\":0,\"x\":0,\"y\":0,\"label\":\"btn1\"},{\"type\":\"OpenLayout\",\"originX\":\"left\",\"originY\":\"top\",\"left\":13,\"top\":335,\"width\":200,\"height\":100,\"fill\":\"#f05a30\",\"stroke\":null,\"strokeWidth\":1,\"strokeDashArray\":null,\"strokeLineCap\":\"butt\",\"strokeLineJoin\":\"miter\",\"strokeMiterLimit\":10,\"scaleX\":1,\"scaleY\":1,\"angle\":0,\"flipX\":false,\"flipY\":false,\"opacity\":1,\"shadow\":null,\"visible\":true,\"clipTo\":null,\"backgroundColor\":\"\",\"rx\":0,\"ry\":0,\"x\":0,\"y\":0,\"label\":\"\"}],\"background\":\"\"}"
下面是表示结构的类:

public partial class ControlPageResponse
{

    [JsonProperty("objects")]
    public CanvasBtns[] Btns { get; set; }

    [JsonProperty("background")]
    public string Background { get; set; }
}

public class CanvasBtns
{

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("originX")]
    public string OriginX { get; set; }

    [JsonProperty("originY")]
    public string OriginY { get; set; }

    [JsonProperty("left")]
    public int Left { get; set; }

    [JsonProperty("top")]
    public int Top { get; set; }

    [JsonProperty("width")]
    public int Width { get; set; }

    [JsonProperty("height")]
    public int Height { get; set; }

    [JsonProperty("fill")]
    public string Fill { get; set; }

    [JsonProperty("stroke")]
    public object Stroke { get; set; }

    [JsonProperty("strokeWidth")]
    public int StrokeWidth { get; set; }

    [JsonProperty("strokeDashArray")]
    public object StrokeDashArray { get; set; }

    [JsonProperty("strokeLineCap")]
    public string StrokeLineCap { get; set; }

    [JsonProperty("strokeLineJoin")]
    public string StrokeLineJoin { get; set; }

    [JsonProperty("strokeMiterLimit")]
    public int StrokeMiterLimit { get; set; }

    [JsonProperty("scaleX")]
    public int ScaleX { get; set; }

    [JsonProperty("scaleY")]
    public int ScaleY { get; set; }

    [JsonProperty("angle")]
    public int Angle { get; set; }

    [JsonProperty("flipX")]
    public bool FlipX { get; set; }

    [JsonProperty("flipY")]
    public bool FlipY { get; set; }

    [JsonProperty("opacity")]
    public int Opacity { get; set; }

    [JsonProperty("shadow")]
    public object Shadow { get; set; }

    [JsonProperty("visible")]
    public bool Visible { get; set; }

    [JsonProperty("clipTo")]
    public object ClipTo { get; set; }

    [JsonProperty("backgroundColor")]
    public string BackgroundColor { get; set; }

    [JsonProperty("rx")]
    public int Rx { get; set; }

    [JsonProperty("ry")]
    public int Ry { get; set; }

    [JsonProperty("x")]
    public int X { get; set; }

    [JsonProperty("y")]
    public int Y { get; set; }

    [JsonProperty("label")]
    public string Label { get; set; }
}
}

当我尝试反序列化时,我得到“Newtonsoft.Json.JsonSerializationException类型的第一次意外异常发生在Newtonsoft.Json.dll中[…]转换值[…]时出错”“ControlPageDesigner.General.ControlPageResponse.Path'”。我似乎找不到问题所在

ControlPageResponse controlPageRecord = JsonConvert.DeserializeObject<ControlPageResponse>(ControlPage);
ControlPageResponse controlPageRecord=JsonConvert.DeserializeObject(ControlPage);

我不确定下面的猜测,因为我还没有在ASP.NET中使用fabricjs 但是,如果使用javascript序列化fabric.canvas-object,它与其他javascript对象不同,因为在框架中,toJSON方法被覆盖。 如果调用toJSON,fabirc.canvas对象只返回表示其内容的对象。序列化此返回对象后,将获得画布内容的json表示。而不是画布对象本身。 对于反序列化,您必须使用一种特殊的方法(loadFromJSON


我认为使用ASP.NET的行为将是相同的。

我遇到了与此类似的问题,并发现我需要在JSON对象中声明一个空构造函数。否则,反序列化程序将尝试使用我的另一个构造函数并给出上述错误。

这是一个非常奇怪的错误。我创建了一个新的控制台应用程序,并将您的代码和JSON剪切粘贴到其中。它运行正常,并将JSON反序列化到对象中。我从NuGet获取了最新版本的JSON.NET。也许您使用的是不能正确处理数据的旧版本?我假设您尝试反序列化的变量(
ControlPage
)是一个字符串变量,包含您在文章中包含的JSON。我使用的是MVC4中包含的内置JSON.NET。我的应用程序似乎正在运行JSON.NET 5.0.8。我会更新,看看它是否有效!为什么在课堂上使用
Type
Type??为什么不是它的全名?这可能就是问题所在