使用数据契约将 JSON 转换为 C# 对象 - 我在这里缺少什么?

发布于 2024-11-19 19:41:36 字数 2065 浏览 0 评论 0原文

我在将 JSON 转换为强类型类时遇到错误。

我的 JSON: {"listBoxID":"ctl00_ctl00_MainContentRoot_MainContent_lstBxSettings","sourceItemText":"Horizo​​ntal Bar","sourceItemValue":"Horizo​​ntal"}

DroppedItem droppedItem = JsonConvert.DeserializeObject<DroppedItem>(json);

/// <summary>
/// Outlines an object which is useful in simplifying how a CormantRadDock is created.
/// Instead of passing in lots of parameters, would rather just pass in an object that the
/// CormantRadDock knows how to interpret.
/// </summary>
[DataContract]
public class DroppedItem
{
    private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    [DataMember(Name = "sourceItemText")]
    public string Text { get; set; }

    [DataMember(Name = "sourceItemValue")]
    public string Value { get; set; }

    [DataMember(Name = "listBoxID")]
    public Reports ReportType { get; set; }

    public DroppedItem() { }

    public DroppedItem(string text, string value, string listBoxID)
    {
        Logger.DebugFormat("Text: {0}, Value: {1}, slidingPaneTitle: {2}", text, value, listBoxID);
        Text = text;
        Value = value;
        ReportType = DetermineReportType(listBoxID);
    }

    private Reports DetermineReportType(string listBoxID)
    {
        if (listBoxID.Contains("lstBxHistorical"))
        {
            return Reports.HistoricalReport;
        }
        else if (listBoxID.Contains("lstBxCustom"))
        {
            return Reports.CustomReport;
        }
        else
        {
            return Reports.None;
        }
    }
}

问题在于将 listBoxID 转换为 ReportType。

未捕获的 Sys.WebForms.PageRequestManagerServerErrorException:Sys.WebForms.PageRequestManagerServerErrorException:将值“ctl00_ctl00_MainContentRoot_MainContent_lstBxSettings”转换为类型“CableSolve.Web.Reports”时出错

无论 if 语句是否找到命中或默认为 else 块,都会发生这种情况。如果我不尝试传递 listBoxID 参数,则不会发生这种情况。

我在这里缺少一些东西。我的 DataMember 名称没有执行任何操作吗?我认为他们会将 listBoxID 映射到正确的属性。

I am experiencing an error converting JSON to a strongly-typed class.

My JSON: {"listBoxID":"ctl00_ctl00_MainContentRoot_MainContent_lstBxSettings","sourceItemText":"Horizontal Bar","sourceItemValue":"Horizontal"}

DroppedItem droppedItem = JsonConvert.DeserializeObject<DroppedItem>(json);

/// <summary>
/// Outlines an object which is useful in simplifying how a CormantRadDock is created.
/// Instead of passing in lots of parameters, would rather just pass in an object that the
/// CormantRadDock knows how to interpret.
/// </summary>
[DataContract]
public class DroppedItem
{
    private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    [DataMember(Name = "sourceItemText")]
    public string Text { get; set; }

    [DataMember(Name = "sourceItemValue")]
    public string Value { get; set; }

    [DataMember(Name = "listBoxID")]
    public Reports ReportType { get; set; }

    public DroppedItem() { }

    public DroppedItem(string text, string value, string listBoxID)
    {
        Logger.DebugFormat("Text: {0}, Value: {1}, slidingPaneTitle: {2}", text, value, listBoxID);
        Text = text;
        Value = value;
        ReportType = DetermineReportType(listBoxID);
    }

    private Reports DetermineReportType(string listBoxID)
    {
        if (listBoxID.Contains("lstBxHistorical"))
        {
            return Reports.HistoricalReport;
        }
        else if (listBoxID.Contains("lstBxCustom"))
        {
            return Reports.CustomReport;
        }
        else
        {
            return Reports.None;
        }
    }
}

The issue is with converting listBoxID to ReportType.

Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Error converting value "ctl00_ctl00_MainContentRoot_MainContent_lstBxSettings" to type 'CableSolve.Web.Reports'

It occurs regardless of whether the if statement finds a hit or defaults to the else block. It does not occur if I do not attempt to pass the listBoxID parameter.

I'm missing something here. Are my DataMember names not doing anything? I thought they would map the listBoxID to the correct property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-11-26 19:41:36

更改为如下所示:

public Reports ReportType { get; set; }


[DataMember(Name = "listBoxID")]
public string listBoxID 
{
    set
    {
         ReportType = DetermineReportType(value);
    } 

}

因为基本上,您可以将该 string 转换为 Report 而无需使用辅助方法。反序列化时未调用构造函数

Change to something like this:

public Reports ReportType { get; set; }


[DataMember(Name = "listBoxID")]
public string listBoxID 
{
    set
    {
         ReportType = DetermineReportType(value);
    } 

}

Because basically, you can convert that string to a Report without your helper method. The constructor is not being called on deserialization

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文