C# 如何使用标准值初始化包含 list<T> 的对象

标签 c# json

此问题与 this 相关问题。我设法更进一步,但现在无法使用默认值初始化整个对象,以防止它在列表级别为空。这样做的目的是将“null”值传递给我的 SQL 查询。最终我想要的是我的数据库中的一条记录将表达:该行已被记录,但相关值为“null”。

我尝试过 Brian 的 fiddle ,但用标准值初始化整个模型似乎对我不起作用。

期望:在对象初始化时,应使用“null”值,然后覆盖该值,以防存在通过 JSON 反序列化产生的值。

这是我尝试过的。所有这些都不会达到预期的效果。我收到此错误:

Application_Error: System.ArgumentNullException: Value cannot be null.

每次我尝试访问数据模型中的列表之一时。

namespace Project.MyJob
{
 public class JsonModel
 {
    public JsonModel()
    {
        Type_X type_x = new Type_X(); // This works fine.
        List<Actions> action = new List<Actions>(); // This is never visible 
        /*in my object either before I initialise JObject or after. So whatever 
        gets initialised here never makes it to my object. Only Type_X appears 
        to be working as expected. */
        action.Add(new Actions {number = "null", time = "null", station = 
        "null", unitState = "null"}) // This also does not prevent my 
        //JsonModel object from being null.
    }
    public string objectNumber { get; set; }
    public string objectFamily { get; set; }
    public string objectOrder { get; set; }
    public string location { get; set; }
    public string place { get; set; }
    public string inventionTime { get; set; }
    public string lastUpdate { get; set; }
    public string condition { get; set; }

    public Type_X Type_X { get; set; }
    public List<Actions> actions { get; set; }         
 }

 public class Actions
 {
    public Actions()
    {
      // None of this seems to play a role at inititialisation.
      count = "null";
      datetime = "null";
      place = "null";
      status = "null";
    }

    // public string count { get; set; } = "null"; should be the same as above 
      // but also does not do anything.
    public string count { get; set; }
    public string datetime { get; set; }
    public string place { get; set; }
    public string status { get; set; }
 }

 public class Type_X
 {  
    public Type_X
    {
      partA = "null"; // This works.
    }

    public string partA { get; set; }

    public string PartB { get; set; }

    public string partC { get; set; }

    public string partD { get; set; }

    public string partE { get; set; }
  }
}

这就是我现在根据 Brian 的回答初始化对象的方式。

JObject = JsonConvert.DeserializeObject< JsonModel >(json.ToString(), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

当我尝试迭代 Actions 的内容时,它(逻辑上)给出了上述空错误。

for (int i = 0, len = JObject.actions.Count(); i < len; i++)

我目前对构造函数初始化的理解:

  • 如果我定义诸如 count = "null"; 之类的值,它们应该出现在创建的任何新对象中。
  • 如果存在默认值,我也会期望包含具有默认值的项目(例如,例如 count)的列表将为 Count() 1 并且不为 null。这怎么可能?

最佳答案

这会让你摆脱束缚:

private List<Actions> _actions = new List<Actions>();
public List<Actions> actions { get => _actions; set => _actions = value ?? _actions; }

这会导致尝试将操作设置为 null将其设置为先前的值,并且最初不是 null所以它永远不可能null .

我不确定我是否正确地阅读了您的问题,因此这是 A 部分的相同片段:

private string _partA = "null";
public string partA { get => _partA; set => _partA = value ?? _partA; }

关于C# 如何使用标准值初始化包含 list<T> 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47965919/

相关文章:

json - 如何将数据序列化为 JSON 对象

javascript - 获取 JSON 字符串化值

c# - 为什么此代码使用异步方法?

c# - 在线获取错误 "The remote server returned an error: (400) Bad Request."WebResponse response = request.GetResponse();

c# - 将二维整数作为只读/常量存储在单独的类中,同时保持不公开

c# - 返回给调用者的异步结果句柄

C# Http 基本身份验证绕过

javascript - angular.fromJson 和 JSON.parse 有什么区别?

java - Java 1.4 的 GSON 版本

javascript - jQuery:打印嵌套数组中多个键的所有值