帮助反序列化字典从ASP.NET页返回到C#App [英] Help Deserializing Dictionary returned from ASP.NET page to C# App

查看:104
本文介绍了帮助反序列化字典从ASP.NET页返回到C#App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搞一个拼写检查器,但遇到了一个问题:将拼写错误的单词及其建议拼写的字典反序列化.应用程序将文本发送到ASP.NET页. ASP应用程序执行拼写检查,并返回字典,其中的键是拼写错误的单词以及正确建议列表.
调用ser.Deserialize时在VS 2010中收到的错误:

I am messing around with a spell checker and I am having a problem De-serializing a dictionary of misspelled words and their suggested spellings. An application sends text to an ASP.NET page. The ASP app performs spell checking and returns a dictionary with the Keys being the misspelled words along with a List of correct suggestions.
Error that I am receiving in VS 2010 when ser.Deserialize is called:

An unhandled exception of type ''System.ArgumentException'' occurred in System.Web.Extensions.dll
Additional information: Invalid object passed in, member name expected. (1): {"missspelled":["misspelled","miss spelled","miss-spelled","misspelling","misspell","dispelled","misspecified"],"wrods":["words","rods","woods","prods","w rods"]}



对于我一生,我无法弄清楚运行时为什么会抱怨.任何帮助将不胜感激.

ASP.NET代码



For the life of me I cannot figure out why the runtime is complaining. Any help would be appreciated.

ASP.NET code

Dictionary<string, List<string>> WordSuggestions = new Dictionary<string, List<string>>();
...
Populate dictionary with misspelled words and list of suggestions
...
//JSON encode Dictionary and return.
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonresult = Server.HtmlEncode(serializer.Serialize(WordSuggestions));
Response.Write(jsonresult);



App看到/接收的数据



Data as seen/received by App

{&quot;missspelled&quot;:[&quot;misspelled&quot;,&quot;miss spelled&quot;,&quot;miss-spelled&quot;,&quot;misspelling&quot;,&quot;misspell&quot;,&quot;dispelled&quot;,&quot;misspecified&quot;],&quot;wrods&quot;:[&quot;words&quot;,&quot;rods&quot;,&quot;woods&quot;,&quot;prods&quot;,&quot;w rods&quot;]}



C#应用程序代码(如上所示,ReturnedSpelling保留了应用程序看到的数据".



C# App Code (ReturnedSpelling holds the "Data as seen by App" as seen above.

JavaScriptSerializer ser = new JavaScriptSerializer();
MispelledWords.text = ser.Deserialize<Dictionary<string, string[]>>(ReturnedSpelling);



MispelledWords.text是类型适当的字典



MispelledWords.text is an appropriate dictionary of type

Dictionary<string, string[]>;



我刚注意到的一件事是,在ASP应用程序中,我的字典是:



One thing I just noticed is that in the ASP app, my Dictionary is:

Dictionary<string, List<string>> 


而我的C#应用​​是:


and my C# app is:

Dictionary<string, string[]> 



尽管我看不到序列化后为什么会出现问题.

...

此后,我在应用程序(接收)端更改了字典



Although I can''t see why this would be a problem after it has been serialized.

...

I have since changed my dictionary

string[] to List<string>

.仍然无法正常运作...正如我所怀疑的.我先进行序列化,然后进行编码,然后发送,并在返回数据时按该顺序进行解码和反序列化.

这是我在应用程序端使用的:

编码...

on my application (receiving) side. It still does not work...as I suspected. I am serializing then encoding then sending and when the data is returned, decoding and de-serializing in that order.

This is what I am using on my application side:

Encoding...

HttpUtility.UrlEncode



解码...



Decoding...

HttpUtility.UrlDecode

推荐答案

这些指针很少.试试这个
-是否检查数据是否为编码形式.与序列化后的编码一样,您可能需要在反序列化之前对其进行解码.

-另外,您还在尝试更改Dictionary< string,List< string>>的类型.到字典< string,string []>
这可能是原因.请勿更改类型.您可以使用多种技术对序列进行反序列化后进行更改.

另外,提供一些有关WordSuggestions的数据,以便我们也可以对其进行检查
These are few pointers. Try this
- Did you check the data whether it is encoded form or not. As are encoding after Serializing, you might need to decode this before DeSerializing.

- Also yo are trying to change the type Dictionary<string, List<string>> to Dictionary<string, string[]>
It might be the cause. Don''t change the type. You may change it after DeSerializing using various techniques.

Also, give some data for WordSuggestions, so that we also can check it


重新阅读文章后,我看到了问题.

问题出在这里:

我使用HtmlEncode在ASP.NET中对返回的序列化数据进行编码,但在Windows应用程序中使用了UrlDecode.

糟糕!
After re reading the posting, I saw the issue.

Here was the problem:

I was encoding the returned serialized data in the ASP.NET with HtmlEncode but using UrlDecode in the Windows app.

Oops.


这篇关于帮助反序列化字典从ASP.NET页返回到C#App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆