C# javascript自定义转换器反序列化json数组

C# javascript自定义转换器反序列化json数组,c#,json,C#,Json,我有一个包含long数组的对象模型,我正在反序列化一个json字符串,该字符串包含一个使用自定义javascript转换器和javascript序列化程序类的数组 我原以为这样行得通,但事实并非如此: List<long> TheList = new List<long>; if (dictionary.ContainsKey("TheArray") && dictionary["TheArray"] != null) { TheList = s

我有一个包含long数组的对象模型,我正在反序列化一个json字符串,该字符串包含一个使用自定义javascript转换器和javascript序列化程序类的数组

我原以为这样行得通,但事实并非如此:

List<long> TheList = new List<long>;

if (dictionary.ContainsKey("TheArray") && dictionary["TheArray"] != null)
{
    TheList = serializer.ConvertToType<List<long>>(dictionary["TheArray"]); //bug
    TheObject.TheObjectList = (from s in TheList 
                               select Convert.ToInt64(s)).ToList<long>();
}
但是我得到了这个错误信息:

反序列化数组时不支持类型“System.String”

我错过了什么


谢谢。

数组对
JavaScriptConverter
可见,如
ArrayList
,您可以这样进行反序列化:

List<long> theArray = null;

if (dictionary.ContainsKey("TheArray") && dictionary["TheArray"] is ArrayList)
{
    theArray = new List<long>();
    ArrayList serializedTheArray = (ArrayList)dictionary["TheArray"];
    foreach (object serializedTheArrayItem in serializedTheArray)
    {
        if (serializedTheArrayItem is Int64)
            theArray.Add((long)serializedTheArrayItem);
    }
}
List theArray=null;
if(dictionary.ContainsKey(“TheArray”)&dictionary[“TheArray”]是ArrayList)
{
theArray=新列表();
ArrayList serializedTheArray=(ArrayList)字典[“TheArray”];
foreach(serializedTheArray中的对象serializedTheArrayItem)
{
如果(SerializedTharyItem为Int64)
添加((长)序列化的HearRayItem);
}
}

这将执行所有类型检查,以防JSON中出现意外情况。当然,它假设JSON中的Array属性实际上包含一个数组,而不是表示数组的内部JSON字符串(错误消息可能表明存在这种问题)。

数组(字符串?)中的所有值都是有效的Int64值吗?是的,它们都是javascript int值。作为预防措施,我实际上还在下一行将值解析为Int64。请向我们展示您的json json是一个包含数字数组的字典,都是经典的,没有什么特别的。好的,我可以让您的serializedTheArray包含数组。现在我正在用linq编写第二行,就像我在问题中看到的一样,而不是foreach循环。
List<long> theArray = null;

if (dictionary.ContainsKey("TheArray") && dictionary["TheArray"] is ArrayList)
{
    theArray = new List<long>();
    ArrayList serializedTheArray = (ArrayList)dictionary["TheArray"];
    foreach (object serializedTheArrayItem in serializedTheArray)
    {
        if (serializedTheArrayItem is Int64)
            theArray.Add((long)serializedTheArrayItem);
    }
}