我有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());
我期待AdvertiserLookup
下面的所有键和文字的Json(非常好的不必要)。
我在test.txt
中得到了什么:
[AdvertiserLookup System.Collections.Generic.Dictionary``2[System.String,System.Object]]
如何将整个内容写入文本?
答案 0 :(得分:2)
尝试Json.NET。您应该可以序列化字典。
var jsonString = JsonConvert.SerializeObject(advertiserResponse, Formatting.Indented)