无法序列化对象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));
返回[{},{}]
我该如何解决这种不幸的情况?使用类似于“元组”的意思,结果没有给出。
答案 0 :(得分:1)
您的示例不是编译栏。试试这个:
var writer = new JsonWriter();
Console.WriteLine( writer.Write(test_d));
我有输出:
{ “温度”:12, “TEMP2”:[0,1,2,3,4,5]}
答案 1 :(得分:1)
我知道答案是关于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
]
}
]