无法序列化对象KeyValuePair。例如:
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 [{},{}]如何修复这种不幸的情况?在“元组”的含义内使用相似的结果没有给出。
发布于 2017-01-16 16:14:01
您的示例不是编译栏。试试这个:
var writer = new JsonWriter();
Console.WriteLine( writer.Write(test_d));我有以下输出:
{"temp":12,"temp2":0,1,2,3,4,5}
发布于 2017-01-16 16:16:55
我知道答案是关于JsonFX,但您也可以使用Json.NET并将其序列化,如下所示:
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
});结果将是:
[
{
"Key": "temp",
"Value": 12
},
{
"Key": "temp2",
"Value": [
0,
1,
2,
3,
4,
5
]
}
]https://stackoverflow.com/questions/41671885
复制相似问题