我已尝试过多种方法来解析Windows 8中的json,并且我收到了很多错误。
“WinRT信息:WEB_E_INVALID_JSON_STRING”
我得到了一个点,我使用相同的json字符串,如果我从网上读取它,它可以(或多或少)工作,但如果我从本地文件中读取它将无法工作。
以下是从网上阅读的代码:
public async void ExamineJson()
{
string responseText = await GetjsonStream();
ParseJson(responseText);
}
public async Task<string> GetjsonStream()
{
HttpClient client = new HttpClient();
string url = "http://rmarinho.facilit.us/app/d/rtp/config.json";
HttpResponseMessage response = await client.GetAsync(url);
return response.Content.ReadAsString();
}
private static void ParseJson(string responseText)
{
JsonObject root = new JsonObject(responseText);
string epg = root.GetNamedString("epgurl");
JsonArray themes = root["themes"].GetArray();
for (int i = 0; i < themes.Count; i++)
{
JsonObject section = themes[i].GetObject();
}
}
所以这可行,但是如果我使用相同的解析方法并使用此代码从我的应用程序中的本地文件获取文件,如果失败并显示错误“WinRT信息:WEB_E_INVALID_JSON_STRING”。
FileSync.Read<string>(installedLocation, "config.json",
(fileSize, reader) =>
reader.ReadString(fileSize),
responseText =>
{
ParseJson(responseText);
})
public static class FileSync
{
public static async void Read<TDocument>(StorageFolder folder, string fileName,
Func<uint, DataReader, TDocument> reader, Action<TDocument> completion = null)
{
StorageFile file;
IRandomAccessStream stream;
IInputStream inputStream;
DataReader dr;
file = await folder.GetFileAsync(fileName);
stream = await file.OpenAsync(FileAccessMode.Read);
inputStream = stream.GetInputStreamAt(0);
uint fileSize = (uint)stream.Size;
dr = new DataReader(inputStream);
await dr.LoadAsync(fileSize);
Task<TDocument> task = new Task<TDocument>(() => reader(fileSize, dr));
task.Start();
TDocument doc = await task;
if (completion != null)
{
completion(doc);
}
}
public static async void Write<TDocument>(StorageFolder folder, string fileName,
CreationCollisionOption collisionOption, TDocument doc,
Action<DataWriter, TDocument> writer, Action<bool> complete = null)
{
StorageFile creator;
IRandomAccessStream stream;
IOutputStream outputStream;
DataWriter dw;
creator = await folder.CreateFileAsync(fileName, collisionOption);
stream = await creator.OpenAsync(FileAccessMode.ReadWrite);
outputStream = stream.GetOutputStreamAt(0);
dw = new DataWriter(outputStream);
Task task = new Task(() => writer(dw, doc));
task.Start();
await task;
await dw.StoreAsync();
bool success = await outputStream.FlushAsync();
if (complete != null)
{
complete(success);
}
}
}
任何人都可以帮我解决一下如果这是预览版的错误,或者是我缺少的东西?!
提前致谢
答案 0 :(得分:1)
想出来了。当我从文件中读取JSON时,我用来读取文件的方法是将UTF8 ByteOrderMark字符复制到结果流中。结果,当我调用stringFromFile.equals(hardcodedString)时返回false。我使用以下代码从文件中读取文本并剥离BOM,现在使用Windows.Data.Json解析JSON。
private readonly static string UTF8_BYTE_ORDER_MARK =
Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble(), 0, Encoding.UTF8.GetPreamble().Length);
private string GetStringContentsOfFile(string path)
{
Uri filePath = new Uri(path);
var jsonFileTask = StorageFile.GetFileFromApplicationUriAsync(filePath).AsTask();
jsonFileTask.Wait();
var jsonFile = jsonFileTask.Result;
var getStringContentsTask = FileIO.ReadTextAsync(jsonFile, Windows.Storage.Streams.UnicodeEncoding.Utf8).AsTask();
getStringContentsTask.Wait();
var text = getStringContentsTask.Result;
// FileIO.ReadTextAsync copies the UTF8 byte order mark into the result string. Strip the byte order mark
text = text.Trim(UTF8_BYTE_ORDER_MARK.ToCharArray());
return text;
}