简体   繁体   English

声明“可空<string> []" 或 "string[]?" 对于 class 中可能存在也可能不存在的字符串数组属性?</string>

[英]Declare "Nullable<string>[]" or "string[]?" for string array property that may or may not exist inside a class?

I have a JSON file like this:我有一个这样的 JSON 文件:

[
  {
    "Id": 1,
    "Size": "big",
    "Order": 6
  },
  {
    "Id": 2,
    "Size": "small",
    "Order": 4
  },
  {
    "Id": 3,
    "Size": "medium",
    "Order": 2,
    "chips": []
  }
]

The chips property is an array that may or may not appear in some object (It is currently null at this point). chips属性是一个数组,可能会出现也可能不会出现在一些object中(目前是null)。 Should I declare the class for the json file like this:我是否应该像这样为 json 文件声明 class:

 public class Settings
    { 
        public int Id { get;}
        public string Size { get;}
        public int Order { get;}
        public string[]? Chips { get;}
    }

with the?与? or something like Nullable[] for the property instead?或类似 Nullable[] 的属性?

In short: you don't need Nullable<T> or ?简而言之:您不需要Nullable<T>? in this case at all.在这种情况下。

string[] is reference type: string[]是引用类型:

Console.WriteLine(typeof(string[]).IsValueType);

the printed output will be false .打印的 output 将是false

So, it can be null without any decoration.因此,它可以是null ,无需任何修饰。


Back to your sample.回到你的样本。 You need to specify setters as well to be able deserialize the given json fragement:您还需要指定 setter 才能反序列化给定的 json 片段:

public class Settings
{
    public int Id { get; set; }
    public string Size { get; set; }
    public int Order { get; set; }
    public string[] Chips { get; set; }
}

Because the top-level entity is not an object that's why you need to use JArray to parse it first and then convert it to Settings via the ToObject ( 1 ):因为顶级实体不是 object,所以您需要先使用JArray解析它,然后通过ToObject ( 1 ) 将其转换为Settings

var json = "[\r\n  {\r\n    \"Id\": 1,\r\n    \"Size\": \"big\",\r\n    \"Order\": 6\r\n  },\r\n  {\r\n    \"Id\": 2,\r\n    \"Size\": \"small\",\r\n    \"Order\": 4\r\n  },\r\n  {\r\n    \"Id\": 3,\r\n    \"Size\": \"medium\",\r\n    \"Order\": 2,\r\n    \"chips\": []\r\n  }\r\n]";
var semiParsedData = JArray.Parse(json);
var settings = semiParsedData.ToObject<Settings[]>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Deserializing a JSON string to a C# class where JSON class may accept either array / object based on a JSON property - Deserializing a JSON string to a C# class where JSON class may accept either array / object based on a JSON property 正则表达式替换可能被引用或可能不被引用的字符串 - Regex to replace a string that may or may not be quoted 解析可能具有或不具有偏移量的日期时间字符串 - Parsing a datetime string that may or may not have an offset 排列一系列可能为空的字符串变量的最简洁方法是什么 - What is the most concise way to array a series of string variables that may be empty 安全地截断C#中可能为“null”的字符串 - Safely truncating a string in C# that may be `null` 从可能是相对的URL获取查询字符串 - Get query string from URL that may be relative 判断可能出现的字符串赋值运算符异常 - Determine string assignment operator exception that may occur Environment.GetCommandLineArgs() 可能会得到一个不受信任的字符串 - Environment.GetCommandLineArgs() may get an untrusted string 列表功能错误 <String> :“属性或索引器可能不会作为out或ref参数传递” - Function error for List<String>: 'a property or indexer may not be passed as an out or ref parameter' 为什么不能在 class 中声明 const static 字符串 - why cannot declare const static string inside a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM