-1
JsonSerializer serializer = new JsonSerializer();
jw.WritePropertyName("info"); 
serializer.Serialize(jw, res);
if (res[2] == true)
{
    jw.WritePropertyName("healer"); jw.WriteStartObject();
    jw.WritePropertyName("running"); 
    jw.WriteValue(res[1] == true ? (checkBox1.IsChecked == true ? true : false) : false);
    jw.WritePropertyName("main"); jw.WriteStartObject();
    serializer.Serialize(jw, new bool[] { true, true, true, true, true, true });
    jw.WriteEndObject(); // here
    jw.WriteEndObject();
}

有人可以告诉我为什么这段代码不起作用吗?没有线serializer.Serialize(jw, new bool[] {...}});一切正常。

4

1 回答 1

1

异常的原因是您告诉 writer 您将编写一个对象并且您正在编写一个数组。试试这样:

jw.WritePropertyName("healer");
jw.WriteStartObject();
jw.WritePropertyName("running"); 
jw.WriteValue(res[1] == true ? (checkBox1.IsChecked == true ? true : false) : false);
jw.WritePropertyName("main");
serializer.Serialize(jw, new bool[] { true, true, true, true, true, true });
jw.WriteEndObject();

或者作为这样的替代方案:

jw.WritePropertyName("healer");
jw.WriteStartObject();
jw.WritePropertyName("running"); 
jw.WriteValue(res[1] == true ? (checkBox1.IsChecked == true ? true : false) : false);
jw.WritePropertyName("main");
var myArray = new bool[] { true, true, true, true, true, true };
jw.WriteStartArray();
foreach (bool myValue in myArray)
{
    jw.WriteValue(myValue);
}
jw.WriteEndArray();
jw.WriteEndObject();
于 2012-11-21T15:03:15.207 回答