私はWindows 8でjsonを解析するために複数の方法を試しましたが、このエラーが多く発生しました。Json Windows8を解析する
「WinRTの情報:WEB_E_INVALID_JSON_STRING」
それは私が同じJSON文字列を使用するポイントに到達すると、私はウェブからそれから読めば、それは(多かれ少なかれ)動作しますが、それは動作しません。もし私がローカルファイルからそれを読んでいれば。 HERESに
ウェブからそれを読むためのコード:
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();
}
}
だから、これは動作しますが、私は同じ解析メソッドを使用すると、エラーfailswith場合は私のアプリでは、ローカルファイルからファイルを取得するには、このコードを使用している場合"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);
}
}
}
これはプレビュー版のバグか迷っている人なら誰でも助けてくれますか?
ありがとうございます。
単体テスト・テストは機能しませんか? – leppie
あなたはそれが同じ文字列であることは間違いありませんか?あなたのファイルはどのエンコーディングを使用していますか? –
はい私は同じ文字列です..私はvisualyと文字列のようなコードで比較していますxFromserver ==文字列yFromlocalはtrueです... ローカルソースからファイルを読み込むと、ファイルcontente型で何かがあるかもしれないと思いました。 それは唯一の問題ではないようです:http://phil-it.org/chris/?p=769 –