首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KeyValuePair不使用JsonFx序列化

KeyValuePair不使用JsonFx序列化
EN

Stack Overflow用户
提问于 2017-01-16 16:07:21
回答 2查看 105关注 0票数 1

无法序列化对象KeyValuePair。例如:

代码语言:javascript
复制
int[] test = { 0, 1, 2, 3, 4, 5 };
List<KeyValuePair<string, object>> test_d = new List<KeyValuePair<string,object>>();
test_d.Add(new KeyValuePair<string, object>("temp", 12));
test_d.Add(new KeyValuePair<string, object>("temp2", test));
Console.WriteLine(JsonFx.JsonWriter.Serialize(test_d));

Return [{},{}]如何修复这种不幸的情况?在“元组”的含义内使用相似的结果没有给出。

EN

回答 2

Stack Overflow用户

发布于 2017-01-16 16:14:01

您的示例不是编译栏。试试这个:

代码语言:javascript
复制
var writer = new JsonWriter();
Console.WriteLine( writer.Write(test_d));

我有以下输出:

{"temp":12,"temp2":0,1,2,3,4,5}

票数 1
EN

Stack Overflow用户

发布于 2017-01-16 16:16:55

我知道答案是关于JsonFX,但您也可以使用Json.NET并将其序列化,如下所示:

代码语言:javascript
复制
int[] test = {0, 1, 2, 3, 4, 5};
List<KeyValuePair<string, object>> test_d = new List<KeyValuePair<string, object>>();
test_d.Add(new KeyValuePair<string, object>("temp", 12));
test_d.Add(new KeyValuePair<string, object>("temp2", test));


 var x = JsonConvert.SerializeObject(test_d, Formatting.Indented, new JsonSerializerSettings
 {
      TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
 });

结果将是:

代码语言:javascript
复制
[
  {
    "Key": "temp",
    "Value": 12
  },
  {
    "Key": "temp2",
    "Value": [
      0,
      1,
      2,
      3,
      4,
      5
    ]
  }
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41671885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档