如何将字典<字符串、对象>中的所有键和值写入文本文件
本文关键字:键和值 文件 文本 字典 字符串 对象 | 更新日期: 2023-09-27 17:57:08
我有一个看起来像这样的Dictionary<string, object> advertiserResponse
...
"AdvertiserLookup": {
"Agency": {
"AgencyAlpha": "ABC",
"Media": [
{
"Code": "123",
"Name": "XYZ",
"Advertisers": {
"Advertiser": [
{
"Code": "JKL",
"Name": "EFG",
"BusinessKey": "HIJ"
},
{
"Code": "KLM",
"Name": "NOP",
"BusinessKey": "QRS"
},
{
这是我用来尝试写入文本文件的行:
File.WriteAllLines("test.txt", advertiserResponse.Select(x => "[" + x.Key + " " + x.Value + "]").ToArray());
我期待 Json(在漂亮的印刷品中不是必需的),AdvertiserLookup
下面的所有键和文本。我得到的test.txt
:
[AdvertiserLookup System.Collections.Generic.Dictionary``2[System.String,System.Object]]
如何将整个内容写成文本?
试一试 Json.NET。您应该能够序列化字典。
var jsonString = JsonConvert.SerializeObject(advertiserResponse, Formatting.Indented)