WebClient.OpenReadAsync() 损坏 JSON 数据。为什么?
我的 Silverlight 应用程序中有一个类,用于将 JSON 字符串与对象类进行序列化(反序列化)。 我使用 WebClient.OpenReadAsync 来获取包含此 JSON 字符串的文件:
{"FirstName":"Bob","LastName":"Underwood"}
但是,在调用 OpenReadAsync 后,检索到的字符串有很多额外的字符:
"PK\n\0\0\0\0\0�u�>h��5\0\0\05\0\0\0\t\0\0\0test.json\"{\\\"FirstName\\\":\\\"Gary\\\",\\\"LastName\\\":\\\"MacDonald\\\"}\"PK\0\n\0\0\0\0\0�u�>h��5\0\0\05\0\0\0\t\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0test.jsonPK\0\0\0\0\0\07\0\0\0\\\0\0\0\0\0"
这是我的代码我使用下载 JSON:
WebClient client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri("/someJsonFile.zip", UriKind.Relative));
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
if (e.Error == null) {
StreamReader reader = new StreamReader(e.Result);
string jsonString = reader.ReadToEnd().ToString();
}
else {
addMessage("Error " + e.Error.ToString());
}
}
jsonString
最终得到所有额外的数据,所以我无法按原样反序列化它。
另一件需要注意的事情是:URI 指向 someJsonFile.zip
,但它实际上没有压缩,当我给文件提供像 .json
这样的扩展名或没有扩展名时,我得到一个错误,它找不到该文件,但是当我给它一个像 .zip
这样的扩展名时,它发现它很好。有什么办法可以使用普通扩展或无扩展吗?我想知道这是否是配置问题。
问题:
我在提取此文件并使用
StreamReader
获取导致我获取所有垃圾数据的字符串时是否做错了什么?我是否需要做一些特定的事情才能使用
WebClient
获取具有不同扩展名的文件,例如.json
,甚至根本没有扩展名?< /p>
I have a class in my Silverlight app that (de-)serializes JSON strings to/from an object class.
I use WebClient.OpenReadAsync
to get a file that contains this JSON string:
{"FirstName":"Bob","LastName":"Underwood"}
After calling OpenReadAsync
however, the retrieved string has a lot of extra characters:
"PK\n\0\0\0\0\0�u�>h��5\0\0\05\0\0\0\t\0\0\0test.json\"{\\\"FirstName\\\":\\\"Gary\\\",\\\"LastName\\\":\\\"MacDonald\\\"}\"PK\0\n\0\0\0\0\0�u�>h��5\0\0\05\0\0\0\t\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0test.jsonPK\0\0\0\0\0\07\0\0\0\\\0\0\0\0\0"
This is the code I'm using to download the JSON:
WebClient client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri("/someJsonFile.zip", UriKind.Relative));
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
if (e.Error == null) {
StreamReader reader = new StreamReader(e.Result);
string jsonString = reader.ReadToEnd().ToString();
}
else {
addMessage("Error " + e.Error.ToString());
}
}
jsonString
ends up with all that extra data, so I can't deserialize it as is.
Another thing to note: the URI points to someJsonFile.zip
, but it's really not zipped, when I give the file a extension like .json
, or no extension, I get a error that it cannot find the file, but when I give it a extension like .zip
, it finds it fine. Is there a way I can use a normal or no extension? I was wondering if this was a configuration issue.
Questions:
Am I doing something wrong in pulling this file and using
StreamReader
to get the string that's causing me to get all that trash data?Do I need to do something specific to be able to use
WebClient
to grab a file with different extensions, like.json
, or even no extension at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1 - 该数据流肯定是一个 ZIP(
PK
是旧的 PKZip 标记,并且 test.json 文件名也在其索引中提到)。您的服务器可能设置为提供所有压缩文件(或者您可能只是访问实际的 zip 文件)。请检查服务器设置。
2 - 至于第二个问题,WebClient 不关心文件类型。它只是一个数据流,需要由知道数据是什么的东西(即您的代码)来解释。
只有服务器可以配置为以不同的方式提供不同的文件。
1 - That data stream certainly is a ZIP (
PK
is the old PKZip marker and the test.json filename is mentioned in its index as well).Your server may be setup to serve all files compressed (or you may simply be accessing an actual zip file). Please check the server settings.
2 - As for the second question, the WebClient does not care about file types. It is just a stream of data that needs to be interpreted by something that knows what the data is (i.e. your code).
It is only the server that may be configured to serve up different files in different ways.
我能够与我的域名提供商解决问题,似乎是他们的一些配置问题。
I was able to figure things out with my domain provider, appears to have been some configuration issues on their end.