【发布时间】:2021-12-29 20:01:20
【问题描述】:
我正在尝试将一些 JSON 序列化运行到某些对象中。很直接的东西。 出于某种原因,我的第一部分按预期工作,它完美地连载了。
当我将此 JSON 包装在 Deseralize Object 传递的附加元素中时,但该对象为空...我无法发现问题,我似乎无法调试这里发生的事情。
string UIElement = "{\"Name\":\"X1144\",\"Order\":1,\"Tvalue\":\"int\",\"BindValue\":null,\"Default\":null,\"Disabled\":false,\"Mandatory\":true,\"MandatoryMessage\":\"this is a mandatory field\",\"Label\":\"<B> THIS is a HTML</b> label\",\"ToolTip\":\"just a tooltip\",\"PolicyProcess\":[{\"URL\":\"https://google.com\"}],\"Type\":\"RadzenColorPicker\",\"TypeOptions\":[{\"Name\":\"showHSV\",\"Value\":\"True\"},{\"Name\":\"showRGBA\",\"Value\":\"True\"},{\"Name\":\"ShowColors\",\"Value\":\"True\"},{\"Name\":\"showButton\",\"Value\":\"True\"}],\"ItemsType\":null,\"Items\":[],\"Scorable\":false,\"Score\":0,\"Visibility\":true,\"VisibilitySettings\":{\"Condition\":\"OR\",\"Rules\":[{\"Id\":\"X1234\",\"Value\":\"Yes\"},{\"Id\":\"X1222\",\"Value\":\"Yes\"}]},\"KMRs\":false,\"KMRsSettings\":[],\"Docs\":false,\"DocsSettings\":[],\"AutoFill\":true,\"AutoFillSettings\":{\"Value\":\"#999999\",\"Condition\":\"AND\",\"Rules\":[{\"Id\":\"X1234\",\"Value\":\"No\"},{\"Id\":\"X1234\",\"Value\":\"No\"}]}}";
RadzenUIElement element = JsonConvert.DeserializeObject<RadzenUIElement>(UIElement);
Console.WriteLine("UIElement " + element.Name);
foreach (var item in element.VisibilitySettings.Rules)
{
Console.WriteLine( item.Value.ToString());
}
string RadzenPanel = "{\"RadzenPanel\":{\"Id\":\"asfdgsertw34t43t34\",\"RadzenUiElement\":{\"Name\":\"X1144\",\"Order\":1,\"Tvalue\":\"int\",\"BindValue\":null,\"Default\":null,\"Disabled\":false,\"Mandatory\":true,\"MandatoryMessage\":\"this is a mandatory field\",\"Label\":\"<B> THIS is a HTML</b> label\",\"ToolTip\":\"just a tooltip\",\"PolicyProcess\":[{\"URL\":\"https://google.com\"}],\"Type\":\"RadzenColorPicker\",\"TypeOptions\":[{\"Name\":\"showHSV\",\"Value\":\"True\"},{\"Name\":\"showRGBA\",\"Value\":\"True\"},{\"Name\":\"ShowColors\",\"Value\":\"True\"},{\"Name\":\"showButton\",\"Value\":\"True\"}],\"ItemsType\":null,\"Items\":[],\"Scorable\":false,\"Score\":0,\"Visibility\":true,\"VisibilitySettings\":{\"Condition\":\"OR\",\"Rules\":[{\"Id\":\"X1234\",\"Value\":\"Yes\"},{\"Id\":\"X1222\",\"Value\":\"Yes\"}]},\"KMRs\":false,\"KMRsSettings\":[],\"Docs\":false,\"DocsSettings\":[],\"AutoFill\":true,\"AutoFillSettings\":{\"Value\":\"#999999\",\"Condition\":\"AND\",\"Rules\":[{\"Id\":\"X1234\",\"Value\":\"No\"},{\"Id\":\"X1234\",\"Value\":\"No\"}]}}}}";
RadzenPanel panels = JsonConvert.DeserializeObject<RadzenPanel>(RadzenPanel);
Console.WriteLine("elements in panels " + panels.Id);
这段代码的输出如下;
UIElement X1144
Yes
Yes
elements in panels
随着我的进步,我希望增强 RadzenPanel 以保存 UIElements 数组,但现在我无法让它解析单个项目..
对象结构如下所示;
public class RadzenPanel
{
public string Id { get; set; }
public RadzenUIElement RadzenUiElement { get; set; }
}
public class RadzenUIElement
{
public string Name { get; set; }
public int Order { get; set; }
public string Tvalue { get; set; }
public string BindValue { get; set; }
public string Default { get; set; }
public bool Disabled { get; set; }
public bool Mandatory { get; set; }
public string MandatoryMessage { get; set; }
public string Label { get; set; }
public string ToolTip { get; set; }
public PolicyProcessLink[] PolicyProcess{ get; set; }
public string Type { get; set; }
public TypeOptions[] TypeOptions { get; set; }
public string ItemsType { get; set; }
public Items[] Items { get; set; }
public bool Scorable { get; set; }
public double ContributionScore { get; set; }
public bool Visibility { get; set; }
public VisibilitySettings VisibilitySettings { get; set; }
public bool KMRs { get; set; }
public KMRsSettings[] KMRsSettings { get; set; }
public bool Docs { get; set; }
public DocsSettings[] DocsSettings{ get; set; }
public bool AutoFill { get; set; }
public AutoFillSettings AutoFillSettings { get; set; }
}
public class PolicyProcessLink
{
public string URL { get; set; }
}
public class TypeConstants
{
public const string RadzenCheckbox = "RadzenCheckbox";
public const string RadzenCheckboxList = "RadzenCheckboxList";
public const string RadzenColorPicker = "RadzenColorPicker";
public const string RadzenDatePicker = "RadzenDatePicker";
public const string RadzenDropDown = "RadzenDropDown";
}
public class TypeOptions
{
public string Name { get; set; }
public string Value { get; set; }
}
public class Items
{
public string Name { get; set; }
public string Value { get; set; }
public string BGColor { get; set; }
public string TColor { get; set; }
public bool IsScore { get; set; }
public double Score { get; set; }
public bool IsNa { get; set; }
public string KMRIndicator{ get; set; }
public string DocsIncicator { get; set; }
}
public class VisibilitySettings
{
public bool Value { get; set; }
public string Condition { get; set; }
public Rules[] Rules { get; set; }
}
public class Rules
{
public string Id { get; set; }
public string Value { get; set; }
}
public class KMRsSettings
{
public Guid Id { get; set; }
public double Weighting { get; set; }
}
public class DocsSettings
{
public Guid Id { get; set; }
public double Weighting { get; set; }
}
public class AutoFillSettings
{
public string Value { get; set; }
public string Condition { get; set; }
public Rules[] Rules { get; set; }
}
【问题讨论】:
-
你能测试一下,序列化数据然后立即反序列化它,所以字符串是在内存中创建然后使用的吗?您喂它的字符串可能有问题,这只是排除了。
-
好主意,我试试看(y)
-
如果它确实有效,请尝试将序列化结果写入文本文件,然后将其读入或从文本文件复制并粘贴到字符串中。
-
您的
string RadzenPanelJSON 有一个额外的对象容器层,与您的数据模型不对应:{ "RadzenPanel" : { /* RadzenPanel properties */ } }您需要修复您的 JSON 以对应您的数据模型,或者添加一个额外的级别嵌套到数据模型。 -
感谢@dbc,我认为可能是这样,但我不确定我是否发现了问题......