[英]JsonConvert.SerializeObject and handling of Empty Strings
我有一個從包含空字符串的 Web 服務返回的對象。 該對象被稱為Docs
和對象Docs.Rows
是一個List<List<string>>
然后將其在代碼中使用下面作為filteredRows
。 當我使用JsonConvert.SerializeObject
,它會刪除每個包含空字符串的列。 這些列很重要。
我試過這兩種方法:
JsonConvert.SerializeObject(filteredRows,
Formatting.Indented,
new JsonSerializerSettings { });
JsonConvert.SerializeObject(filteredRows,
Formatting.Indented,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });
所有帶有空字符串的列仍被刪除。 我如何讓它保留它們?
FilteredRows 也定義為List<List<string>>
。 我可以將其序列化為定義的對象嗎?
你能提供一個示例程序來演示這個問題嗎? 像下面這樣的簡單示例似乎工作正常。 序列化期間既不會刪除空字符串也不會刪除空字符串。 是否是您的過濾過程實際上正在刪除這些值?
class Program
{
static void Main(string[] args)
{
List<List<string>> rows = new List<List<string>>
{
new List<string>
{
"A",
null,
""
},
new List<string>
{
null,
"B",
""
},
new List<string>
{
"",
null,
"C"
}
};
string json = JsonConvert.SerializeObject(rows);
Console.WriteLine(json);
}
}
輸出:
[["A",null,""],[null,"B",""],["",null,"C"]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.