谁 serialize/deserialize 列表<Tuple<long, string, int>> 使用 JavaScriptSerializer

Who to serialize/deserialize List<Tuple<long, string, int>> using JavaScriptSerializer

我已经使用 JavaScriptSerializer 序列化了 List<Tuple<long, string, int>> obj

JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(dataToSerialize as IEnumerable); 

我得到了字符串:

[{"Item1":0,"Item2":"UserModifiedId","Item3":1},{"Item1":-1,"Item2":"","Item3":0},{"Item1":-1,"Item2":"","Item3":0}]

当我尝试反序列化它时:

JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<List<T>>(strObject);

我得到以下示例:

No parameterless constructor defined for type of 'System.Tuple`3[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.```

Can anyone help me with this Issue (to keep using JavaScriptSerializer)?

尝试在反序列化时指定确切的类型

JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<List<Tuple<long, string, int>>>(strObject);

JavaScriptSerializer 需要一个无参数的构造函数来在反序列化期间创建对象。

因为classTuple有none,反序列化失败。

如果不嫌麻烦可以使用NuGetNewtonsoft.Json,没有他的问题:

var strObject= JsonConvert.SerializeObject(dataToSerialize);

var tupleList = JsonConvert.DeserializeObject<List<Tuple<long, string, int>>>(strObject);