解析JSON C#错误 [英] Parse JSON C# Error
问题描述
我试着让JSON数据,但得到错误
意外遇到字符在解析值。路径',行
0,位置0。
块引用>
我使用的是网4.5和JSON.Net框架
在这里,我的代码WebClient的净=新的WebClient();
字符串str =等待net.DownloadStringTaskAsync(URL);
JObject O = JObject.Parse(STR); //错误在这里,
和我的Webservice的
的 http://sv1.volcanosoft.com/test/index.php?area=ho-智胡志明市
本网站格式的index.php UTF-8和php文件的标题头(内容类型:应用程序/ JSON的;字符集= utf-8');
回声{项:';
回声json_encode($的数据);
回声'};
解决方案下载的字符串以两个的字节顺序标记(U + FEFF),这JSON.NET解析器(正确地)不理解。
为什么下载的字符串包含两个BOM表的原因是因为你的服务发送的数据包含其中的3。第一种是通过UTF-8编码自动删除,但其他两个仍然存在。
BOM可与文件,在那里你不能存储使用的字符集是有用的。但您要发送一个报头使用的字符集,所以你不必送BOM的。和发送他们三个肯定是不正确。
我相信这是由材料明细表在你的PHP文件引起的,所以你应该从那里删除它们。
I'm try to getting JSON data but getting Error.
Unexpected character encountered while parsing value: . Path '', line 0, position 0.
I'm using Net 4.5 and JSON.Net Framework Here 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 Webservice http://sv1.volcanosoft.com/test/index.php?area=ho-chi-minh this 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#错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!