7
我已經嘗試了多種方法來解析JSON在Windows 8中,我得到這個錯誤很多。解析JSON Windows8
「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();
}
}
所以此工程,但如果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 2012-02-01 19:29:57
你確定它是相同的字符串嗎?你的文件使用什麼編碼? – 2012-02-01 21:29:47
肯定是相同的字符串..我有visualy比較,像串碼xFromserver ==串yFromlocal是真的...... 我想知道如果itmaybe一些與文件contente類型,當我讀到從本地源文件.. 看來我不是唯一一個問題:http://phil-it.org/chris/?p=769 – 2012-02-02 15:38:45