JsonArray.Parse(...)错误 [英] JsonArray.Parse(...) error
问题描述
我开发一个新闻应用程序的Windows 8(在C#,XAML)。不幸的是我下载一个JSON进纸(带 http://jsonlint.com/ 确认)异步后遇到一个奇怪的错误。下载成功,然后我想分析的结果: VAR项目= Windows.Data.JsonArray.Parse(结果);
I am developing a news-app for Windows 8 (in C#, XAML). Unfortunately I encountered a strange error after downloading a JSON-Feed (validated with http://jsonlint.com/) asynchronously. The download succeeds and then I want to parse the result: var items = Windows.Data.JsonArray.Parse(result);
.
当我运行code我得到以下错误:
When I run the code I get the following error:
在位置0无效字符。
和无效的JSON字符串。
Json.JsonArray是微软一个新的图书馆。我也试过Newtonsoft的JSON-库同样的错误。我在做什么错了?
Json.JsonArray is a new Library from Microsoft. I also tried Newtonsoft's JSON-library with the same errors. What am I doing wrong?
这是全code:
// Retrieve recipe data from Azure
var client = new HttpClient();
client.MaxResponseContentBufferSize = 1024*1024; // Read up to 1 MB of data
var response = await client.GetAsync(new Uri("http://contosorecipes8.blob.core.windows.net/AzureRecipes"));
var result = await response.Content.ReadAsStringAsync();
// Parse the JSON recipe data
var recipes = JsonArray.Parse(result.Substring(1, result.Length - 1));
这code段是从微软动手实验室(Contoso的食谱)。我也试了一下没有源代码中的[和](无效果)...
This code snippet is from a Microsoft Hands-On Lab (Contoso CookBook). I also tried it without the "[" and "]" in the source (with no effect)...
感谢您!
推荐答案
我可以一个小的修改后,运行code。在UTF8字符串的字节顺序标记似乎引发一个问题,从Windows.Data.Json JsonArray.Parse()。
I was able to run your code after a small modification. The byte order mark of the UTF8 string seems to triggers a problem with JsonArray.Parse() from Windows.Data.Json.
要解决它,而无需使用附加编码的方式是ReadAsStringAsync(),例如。
A way to solve it without using additional encoding is to replace the BOM character after ReadAsStringAsync(), e.g.
result = result.Replace('\xfeff', ' ');
或更好
if (result.Length > 1 && result[0] == '\xfeff'){
result = result.Remove(0, 1);
}
这篇关于JsonArray.Parse(...)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!