【已解决】C#中,Dictionary转换出错:Additional information: Unable to cast object of type ‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’ to type ‘System.Collections.Generic.Dictionary`2[System.String,System.String]’

【问题】

这段C#代码:

//"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-lg._V401028090_.jpg", 
//"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-sm._V401028090_.jpg"

//"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-lg._V400694812_.jpg",
//"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-sm._V400694812_.jpg",
//"rich": {
//    src: "http://g-ecx.images-amazon.com/images/G/01/misc/untranslatable-image-id.jpg",
//    width: null,
//    height: null
//}

Type curType = imgUrlObj.GetType();
Dictionary<string, string> imgUrlDict = (Dictionary<string, string>)imgUrlObj;
string largeImgUrlStr = "";
if (imgUrlDict.TryGetValue("L", out largeImgUrlStr))
{
    imageUrlList[idx] = largeImgUrlStr;
    gotImageUrlOk = true;
}

涉及到,将Object的imgUrlObj,转换为所希望的:Dictionary<string, string>

其中包含的值为:

obj value contain dict strings

对应的其Type类型为:

curType = {Name = "Dictionary`2" FullName = "System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0…

obj type is dictionary 2

结果转换就出错了:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ScrapeAmazonProduct.exe

Additional information: Unable to cast object of type ‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’ to type ‘System.Collections.Generic.Dictionary`2[System.String,System.String]’.

Additional information Unable to cast object of type System.Collections.Generic.Dictionary 2

【解决过程】

1.之前一直没看懂。

现在大概看懂了。

说是不能把:

System.Collections.Generic.Dictionary`2[System.String,System.Object]

转换为:

System.Collections.Generic.Dictionary`2[System.String,System.String]

所以,我们需要把对应的string换成Object:

Type curType = imgUrlObj.GetType();
Dictionary<string, Object> imgUrlDict = (Dictionary<string, Object>)imgUrlObj;
Object largeImgUrObj = "";
if (imgUrlDict.TryGetValue("L", out largeImgUrObj))
{
    imageUrlList[idx] = largeImgUrObj.ToString();
    gotImageUrlOk = true;
}

试试结果,就OK了:

can covert obj to dict string obj

 

【总结】

在将Object转换为原先以为的

Dictionary<string, string>

其实只是针对于这样的内容:

"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-lg._V401028090_.jpg",

"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-sm._V401028090_.jpg"

是可以的。

但是后面还会有别的情况的:

"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-lg._V400694812_.jpg",

"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-sm._V400694812_.jpg",

"rich": {

    src: "http://g-ecx.images-amazon.com/images/G/01/misc/untranslatable-image-id.jpg",

    width: null,

    height: null

}

此时,就必须用

Dictionary<string, Object>

了。否则就会报错:

Unable to cast object of type ‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’ to type ‘System.Collections.Generic.Dictionary`2[System.String,System.String]’.

了。

 

心得是:

要十分注意你所转换的对象的真正的类型,和当前的值。

不要搞错类型了。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量