I have tried multiple ways to parse json in Windows 8 and i m getting this error alot.
"WinRT information: WEB_E_INVALID_JSON_STRING"
It gets to a point that i use the same json string, and it works (more or less) if i read from it from the web, but it won't work if i read it from a local file.
heres the code to read it from the web:
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();
}
}
So this works, but if i use the same parse method and use this code to get file from local file in my app if failswith the error "WinRT information: 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);
}
}
}
Anyone can help me out figure if this a bug from the preview version or is something that i m missing?!
Thanks in advance
Figured this out. When I read JSON from a file, the method I'm using to read the file is copying the UTF8 ByteOrderMark character to the result stream. As a result, when I call stringFromFile.equals(hardcodedString) returns false. I used the following code to read the text from a file and strip the BOM and now parsing the JSON using Windows.Data.Json works.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With