本文介绍了解析JSON C#错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着让JSON数据,但得到错误



I'm using Net 4.5 and JSON.Net FrameworkHere my code

WebClient net = new WebClient();
string str = await net.DownloadStringTaskAsync(url);
JObject o = JObject.Parse(str); // ERROR Here

And my code JSON Data View on Webservicehttp://sv1.volcanosoft.com/test/index.php?area=ho-chi-minhthis site format index.php UTF-8 and header of php file

header('Content-Type:application/json; charset=utf-8');
echo '{"item":';
echo json_encode($data);
echo '}';
解决方案

The downloaded string starts with two byte order marks (U+FEFF), which JSON.NET parser (correctly) doesn't understand.

The reason why the downloaded string contains two BOMs is because the data your service is sending contains 3 of them. The first one is removed automatically by UTF-8 encoding, but the two other remain.

BOM can be useful with files, where you can't store the charset used. But you are sending the charset used in a header, so you don't need to send BOM at all. And sending three of them is certainly incorrect.

I believe this is caused by BOMs in your PHP files, so you should probably remove them from there.

这篇关于解析JSON C#错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:18